I have a Firestore query and, I need it to listen to the creation of new documents, I tried many possibilities but couldn't make it work, every time I create a document the listener is not triggered. This is my listener code:
firestore
.collection("users")
.document("123")
.collection("images")
.whereField("postId", isEqualTo: "123")
.order(by: "createdAt", descending: true)
.limit(to: 1)
.addSnapshotListener { [weak self] snapshot, error in
guard let imageDocument = snapshot?.documents.first else {
return
}
// this print statement is only being called once (not being called when creating a new doc)
print(imageDocument)
}
...
And this is my query to create the document:
firestore
.collection("users")
.document("123")
.collection("images")
.document()
.setData([
"postId": "123",
"imageUrl": "....",
"createdAt": Timestamp()
]) { error in
guard let error = error else { return }
print(error)
}
My goal is that this listener is triggered when I create a new document in the Firestore.
Also, if I remove the limit from my listener query, the code works, but I will spend a lot of reads unnecessarily.
