-
-
Notifications
You must be signed in to change notification settings - Fork 180
Add listener for collection changes #115
-
Hello, how can I add a listener for collection changes?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments 5 replies
-
Using the snapshots flow, see https://github.com/GitLiveApp/firebase-kotlin-sdk#flows
Beta Was this translation helpful? Give feedback.
All reactions
-
OK thanks, so I did something like:
suspend fun observeUsersChanged() {
val firebaseFirestore : FirebaseFirestore = Firebase.firestore(Firebase.app)
val usersCollection = firebaseFirestore.collection("Users")
usersCollection.snapshots.collect {
querySnapshot ->
val documents = querySnapshot.documents
// TODO: get list of my own 'User' object instead of list of DocumentSnapshot
}
}
How can I get a list of my own 'User' object?
I know about data() method of document..but can I get list instead of use for each?
Beta Was this translation helpful? Give feedback.
All reactions
-
And how can I get only the delta? it means-
if I implement a chat application, I want to listen to new messages, but only for the new message/s instead of getting all messages on each event
Beta Was this translation helpful? Give feedback.
All reactions
-
Beta Was this translation helpful? Give feedback.
All reactions
-
First question to ask yourself is how would you do it in firebase in general, once you have that answer I can help suggest best way to do it in the firebase-kotlin-sdk
Beta Was this translation helpful? Give feedback.
All reactions
-
Im not sure how to do it generally because I'm start with firebase with your library...
I can try to explain what I am trying to implement.
I want to create a basic chat application:
Android app - Recycler view for the messages list
Kotlin multiplatform library for common chat code - here I want to implement the communication with Firestore (based your SDK).
So if I already have (for example) 100 messages in my chat..
I am sending a new message and I get 101 documents instead of only 1 new document..
the code for listening is:
override fun observeOnMessagesChanged() {
CoroutineScope(GlobalScope.coroutineContext).launch {
messagesCollection.snapshots.collect { querySnapshot ->
querySnapshot.documents.forEach { documentSnapshot ->
val message = documentSnapshot.data<Message>()
}
}
}
}
Beta Was this translation helpful? Give feedback.
All reactions
-
looks like you need docChanges() which we havent exposed yet https://firebase.google.com/docs/firestore/query-data/listen?authuser=0#view_changes_between_snapshots
You can create a feature request or even help us add it by creating a PR!
Beta Was this translation helpful? Give feedback.
All reactions
-
OK I have opened a feature request here: #122
Hope to find a time to help you too sometimes
Thanks!
Beta Was this translation helpful? Give feedback.