Created
July 23, 2022 05:24
-
-
Save devrath/780c6a17eb9e65e2bb97e02a658fd592 to your computer and use it in GitHub Desktop.
Gist for converting flow
This file contains hidden or 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 getAllPosts() = flow<State<List<Post>>> { | |
| // Emit loading state | |
| emit(State.loading()) | |
| val snapshot = mPostsCollection.get().await() | |
| val posts = snapshot.toObjects(Post::class.java) | |
| // Emit success state with data | |
| emit(State.success(posts)) | |
| }.catch { | |
| // If exception is thrown, emit failed state along with message. | |
| emit(State.failed(it.message.toString())) | |
| }.flowOn(Dispatchers.IO) | |
| // Reference: https://medium.com/firebase-developers/firebase-ing-with-kotlin-coroutines-flow-dab1bc364816 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment