Created
March 5, 2020 23:29
-
-
Save gastsail/5f8847d8d32218ac55ae2e919609adff to your computer and use it in GitHub Desktop.
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
| //1 .- We use callbackFlow , this is like channelFlow and will propagate our data to our viewmodel | |
| override suspend fun getVersionCodeRepo(): Flow<Resource<Int>> = callbackFlow { | |
| // 2.- We create a reference to our data inside Firestore | |
| val eventDocument = FirebaseFirestore | |
| .getInstance() | |
| .collection("params") | |
| .document("app") | |
| // 3.- We generate a subscription that is going to let us listen for changes with | |
| // .addSnapshotListener and then offer those values to the channel that will be collected in our viewmodel | |
| val subscription = eventDocument.addSnapshotListener { snapshot, _ -> | |
| if(snapshot!!.exists()){ | |
| val versionCode = snapshot.getLong("version") | |
| offer(Resource.Success(versionCode!!.toInt())) | |
| } | |
| } | |
| //Finally if collect is not in use or collecting any data we cancel this channel to prevent any leak and remove the subscription listener to the database | |
| awaitClose { subscription.remove() } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment