Skip to content

Instantly share code, notes, and snippets.

@abdurahmanadilovic
Created December 17, 2017 21:07
Show Gist options
  • Save abdurahmanadilovic/7ecf111a161faba5cbf2e17eb1922aa5 to your computer and use it in GitHub Desktop.
Save abdurahmanadilovic/7ecf111a161faba5cbf2e17eb1922aa5 to your computer and use it in GitHub Desktop.
suspendCoroutine example with firebase
override suspend fun getBooleanValue(key: String, default: Boolean): Boolean = suspendCoroutine { continuation ->
fireBaseDb.child("keys").child(key).addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError?) {
continuation.resume(default)
}
override fun onDataChange(p0: DataSnapshot?) {
if (p0 != null) {
continuation.resume(p0.getValue(Boolean::class.java) ?: default)
} else {
continuation.resume(default)
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment