Created
February 8, 2020 11:13
-
-
Save anhtuank7c/ebf45f2cc9d8339d489973e8e5ae2e4a to your computer and use it in GitHub Desktop.
Coroutines concurence query multiple firestore document in Kotlin
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
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