Skip to content

Instantly share code, notes, and snippets.

@a914-gowtham
Last active November 13, 2021 14:35
Show Gist options
  • Save a914-gowtham/a9b1053483e577aa3cc7a54b7ea5a0f1 to your computer and use it in GitHub Desktop.
Save a914-gowtham/a9b1053483e577aa3cc7a54b7ea5a0f1 to your computer and use it in GitHub Desktop.
fun initListener(){
val db = FirebaseFirestore.getInstance()
val groupMsgCollection= db.collectionGroup("group_messages")
groupMsgCollection.whereArrayContains("to", userId!!)
.addSnapshotListener { snapshots, error ->
if (error != null || snapshots == null || snapshots.metadata.isFromCache) {
LogMessage.v("Error ${error?.localizedMessage}")
return@addSnapshotListener
}
onSnapShotChanged(snapshots)
}
}
fun onSnapShotChanged(snapshots: QuerySnapshot){
val messageList= mutableListOf<GroupMessage>()
for(snapShot in snapshots){
val message = snapShot.document.data.toDataClass<GroupMessage>()
messagesList.add(message)
}
groupMessageDao.insertMessages(messageList)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment