The existing solutions are not working for me, I've tried. What I'm trying to do is to fetch messages in a thread that is stored on Firestore. This is the current structure of Chat collection:
As you can see this chat between user 1 and user 2 only has 2 messages and I'm trying to fetch these and also listen to new ones if they are added to thread. Here is the code of how I'm doing it:
func loadChat() {
self.showWaitOverlayWithText("Loading")
let db = Firestore.firestore().collection("Chats")
.whereField("user1ID", isEqualTo: Auth.auth().currentUser?.uid ?? "Not Found User 1")
.whereField("user2UID", isEqualTo: user2UID ?? "Not Found User 2")
db.getDocuments { (querySnap, error) in
if let error = error {
print("Error: \(error)")
return
} else {
let doc = querySnap?.documents.first
doc?.reference.collection("thread").addSnapshotListener(includeMetadataChanges: true, listener: { (threadQuery, error) in
//Never comes here
for message in threadQuery!.documents {
self.removeAllOverlays()
print("Data: \(message.data())")
}
})
}
}
}
It never executes collection("thread").addSnapshotListner
.
if let error = error {
) – David ChopinaddSnapShotListner
part – Chaudhry Talhalet doc = querySnap?.documents.first
is returningnil
. Or, similarly,querySnap
is equal tonil
. – David ChopinUser2ID
notUser2UID
– Chaudhry Talhalet db = Firestore.firestore().collection("Chats").whereField("user1ID", isEqualTo: Auth.auth().currentUser?.uid ?? "Not Found User 1").whereField("user2UID", isEqualTo: user2UID ?? "Not Found User 2
. Can you check whatAuth.auth().currentUser?.uid
anduser2UID
to make sure neither one isnil
and that they are strings that match up with your database? – David Chopin