I am growing a SwiftUI iOS chat utility with Firebase Firestore. Messages despatched by customers aren't showing within the Firestore subcollection designated for chats.
I've confirmed the Firestore information construction is as follows:
chats assortment
chatId doc
messages subcollection
messageId doc
content material: String
id: String (optionally available)
imageUrl: String (optionally available)
lastMessage: String
lastMessageDate: Timestamp
messageType: String
participantIds: Array of Strings
participantNicknames: Array of Strings
senderId: String
senderName: String
sendAt: Timestamp
***
Right here is the simplified code for sending textual content messages:
func sendTextMessage(chatId: String, senderId: String, senderName: String, textual content: String) {
let textMessage = ChatMessage(
id: nil,
senderId: senderId,
messageType: .textual content(textual content),
sentAt: Date(),
senderName: senderName,
content material: textual content
)
do {
strive db.assortment("chats").doc(chatId).assortment("messages").addDocument(from: textMessage)
print("Textual content message despatched.")
} catch {
print("Error sending textual content message: (error)")
}
}
And the code for fetching chat contributors:
func fetchChatParticipants(chatId: String, completion: @escaping ([String]) -> Void) {
db.assortment(“chats”).doc(chatId).getDocument { (doc, error) in
if let doc = doc, doc.exists {
let participantNicknames = doc.information()?[“participantNicknames”] as? [String] ?? []
completion(participantNicknames)
} else {
print(“Doc doesn’t exist: (error?.localizedDescription ?? “Unknown error”)”)
completion([])
}
}
}