https://kotlinlang.org logo
#android
Title
# android
k

Kulwinder Singh

12/11/2019, 4:23 AM
and This class i'm using to convert Firstore snapshot listener to live data, here as i told above listener inside
onActive
is called immedietly but it won't update
value
of live data until
suspending
task inside other
ViewModel
finished
Copy code
class DocumentLiveData<T>(
    private val documentReference: DocumentReference,
    private val typeClass: Class<T>
) : LiveData<T>() {
    override fun onActive() {
        super.onActive()
        listener = documentReference.addSnapshotListener(changes) { documentSnapshot, exception ->
	    //this listener is called but `value` is not updated immedietly
            if (exception == null && documentSnapshot?.exists() == true) {
                value = documentSnapshot.toObject(typeClass)
            } else {
                value = null
            }
        }
    }
}
r

rkeazor

12/11/2019, 12:46 PM
Since your liveData Builder you should convert this callback to a suspendCoroutine or call backflow. And than emit the result in the builder
No need to turn this into a livedata, when livedata gas coroutine support
k

Kulwinder Singh

12/12/2019, 5:49 AM
after testing more i have noticed that if i observe
myLeaderboard
inside MainActivity then it works immediately but if i observe it inside child fragment then it works with delay
6 Views