Last active
April 25, 2023 16:15
-
-
Save saqib-github-commits/190fec63ea93ea7b3dfce9db0d343c5a to your computer and use it in GitHub Desktop.
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 readFirebaseRealtimeDatabaseFlow(): Flow<String> = callbackFlow { | |
val database = FirebaseDatabase.getInstance("<path of realtime database>") | |
val databaseRef = database.reference | |
val firebaseDataListeners = object : ValueEventListener { | |
override fun onDataChange(dataSnapshot: DataSnapshot) { | |
// This method is called once with the initial value and again | |
// whenever data at this location is updated. | |
val value = dataSnapshot.value | |
Log.d(ContentValues.TAG, "Value is: $value") | |
trySend(value?.toString().orEmpty()) | |
} | |
override fun onCancelled(error: DatabaseError) { | |
// Failed to read value | |
Log.w(ContentValues.TAG, "Failed to read value.", error.toException()) | |
} | |
} | |
databaseRef.addValueEventListener(firebaseDataListeners) | |
awaitClose { | |
databaseRef.removeEventListener(firebaseDataListeners) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment