Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save anioutkazharkova/b96eb888fb2d4b0941f1129d2f6da663 to your computer and use it in GitHub Desktop.
Save anioutkazharkova/b96eb888fb2d4b0941f1129d2f6da663 to your computer and use it in GitHub Desktop.
fun startListenToComments(postId: String, result: (List<CommentItem>) -> Unit) {
val collection = FirebaseFirestore.getInstance().collection("posts").document(postId)
.collection("comments")
commentListener = collection.orderBy("date", Query.Direction.DESCENDING)
.addSnapshotListener(MetadataChanges.INCLUDE) { data, firebaseFirestoreExceptioor ->
if (data != null) {
val comments = data.toObjects(CommentItem::class.java)
result(comments)
}
}
}
fun stopCommentListening() {
commentListener?.remove()
commentListener = null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment