Last active
June 23, 2018 16:46
-
-
Save RubyLichtenstein/a3cec00ad741d086f8614d2a45893eba 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
inline fun <reified T> DatabaseReference.toLiveData(): LiveData<T> { | |
val liveData: MutableLiveData<T> = MutableLiveData() | |
addValueEventListener(object : ValueEventListener { | |
override fun onDataChange(dataSnapshot: DataSnapshot) { | |
dataSnapshot.children.forEach { | |
liveData.value = it.getValue<T>(T::class.java) | |
} | |
} | |
override fun onCancelled(databaseError: DatabaseError) { | |
Log.e("DatabaseError", "DatabaseError: " + databaseError.message) | |
} | |
}) | |
return liveData | |
} | |
fun getUsers(): LiveData<User> = usersRef.toLiveData<User>() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment