and This class i'm using to convert Firstore snaps...
# android
k
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
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
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