Created
June 15, 2022 15:16
-
-
Save kostovtd/94414918a98f57a43dea6db018d719a8 to your computer and use it in GitHub Desktop.
A dummy function which shows the usage of callbackFlow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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