Set of Kotlin extension functions used to convert realtime updates from Firebase Firestore collections/documents into LiveData objects (Android Architecture Components).
Gradle:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.brotoo25:firestore-livedata:0.0.9'
}
data class User(val name: String?, val email: String?)
FirebaseFirestore
.getInstance()
.collection("users")
.livedata()
.observe(this, Observer<QueryStatus<QuerySnapshot>> {
Log.d("MainActivity", "${it?.answer?.size()}")
})
When passing the return class type to the 'livedata' function the default DocumentSnapshot.toObject() is called in order to parse the final result.
In case of the default document parser not working for your use case there is also the option to pass a parser function as an argument to handle the mapping behaviour.
FirebaseFirestore
.getInstance()
.collection("users")
.livedata(User::class.java)
.observe(this, Observer<QueryStatus<List<User>>> {
Log.d("MainActivity", "${it?.answer?.size}")
})
OR
FirebaseFirestore
.getInstance()
.collection("users")
.livedata { parseUser(it) }
.observe(this, Observer<QueryStatus<List<User>>> {
Log.d("MainActivity", "${it?.answer?.size}")
})
private fun parseUser(documentSnapshot: DocumentSnapshot) : User {
return User(name = documentSnapshot.getString("name"), email = documentSnapshot.getString("email"))
}
- Create sample app
- Documentation