Create a gist now

Instantly share code, notes, and snippets.

@JosephRoskopfOPI /firebase.kt Secret
Last active Sep 1, 2017

What would you like to do?
Create Firebase Listeners
/**
* Set listener for Firebase
*/
private fun createFirebaseListener(){
val postListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val toReturn: ArrayList<Message> = ArrayList();
for(data in dataSnapshot.children){
val messageData = data.getValue<Message>(Message::class.java)
//unwrap
val message = messageData?.let { it } ?: continue
toReturn.add(message)
}
//sort so newest at bottom
toReturn.sortBy { message ->
message.timestamp
}
setupAdapter(toReturn)
}
override fun onCancelled(databaseError: DatabaseError) {
//log error
}
}
databaseReference?.child("messages")?.addValueEventListener(postListener)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment