Skip to content

Instantly share code, notes, and snippets.

@kostovtd
Created June 15, 2022 15:16
Show Gist options
  • Save kostovtd/94414918a98f57a43dea6db018d719a8 to your computer and use it in GitHub Desktop.
Save kostovtd/94414918a98f57a43dea6db018d719a8 to your computer and use it in GitHub Desktop.
A dummy function which shows the usage of callbackFlow
fun myFunction(id: String): Flow<MyObject> =
callbackFlow {
firestoreEventListener = firestore.collection(MY_COLLECTION_PATH)
.document(id)
.addSnapshotListener { snapshot, _ ->
if (snapshot != null && snapshot.exists()) {
if (!snapshot.metadata.isFromCache) {
snapshot.toObject(MyFirestoreObject::class.java)?.let {
it.id = id
trySend(it)
}
}
}
}
awaitClose {
unsubscribeFirestore()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment