Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add listener for collection changes #115

Closed Unanswered
matanmarciano asked this question in Q&A
Discussion options

Hello, how can I add a listener for collection changes?

You must be logged in to vote

Replies: 3 comments 5 replies

Comment options

You must be logged in to vote
1 reply
Comment options

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?

Comment options

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

You must be logged in to vote
1 reply
Comment options

Comment options

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

You must be logged in to vote
3 replies
Comment options

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>()
 }
 }
 }
 }
Comment options

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!

Comment options

OK I have opened a feature request here: #122
Hope to find a time to help you too sometimes

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #115 on December 14, 2020 13:09.

AltStyle によって変換されたページ (->オリジナル) /