Skip to content

Instantly share code, notes, and snippets.

@anhtuank7c
Created February 8, 2020 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anhtuank7c/ebf45f2cc9d8339d489973e8e5ae2e4a to your computer and use it in GitHub Desktop.
Save anhtuank7c/ebf45f2cc9d8339d489973e8e5ae2e4a to your computer and use it in GitHub Desktop.
Coroutines concurence query multiple firestore document in Kotlin
private fun showMultiStableInfo(stableKeys: Set<Any?>) {
val scope = CoroutineScope(Dispatchers.Default)
scope.launch {
var stables = arrayListOf<Stable>()
stableKeys.forEach { key ->
key?.let { stableID ->
val job = CoroutineScope(Dispatchers.Default).async {
suspendCoroutine<Stable?> { cont ->
myRef.child("stables").child(stableID as String).addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val stable = snapshot.getValue(Stable::class.java)
cont.resume(stable)
}
override fun onCancelled(error: DatabaseError) {
}
})
}
}
val stable = job.await()
stable?.let {
stables.add(stable)
}
}
}
activity?.runOnUiThread {
// do something
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment