Slide 8
Slide 8 text
02
Use coroutines when fetching data
Code After Coroutines
P. 04
Bemefits OF Coroutines
val scope = CoroutineScope(Dispatchers.Main)
scope.launch {
try {
val result = withContext(Dispatchers.IO) {
db.collection("users").get().await()
}
for (document in result) {
Log.d(TAG, "${document.id} => ${document.data}")
val orderResult = withContext(Dispatchers.IO) {
db.collection("orders")
.whereEqualTo("userId", document.id)
.get()
.await()
}
for (orderDocument in orderResult) {
Log.d(TAG, "Order ${orderDocument.id}")
}
val paymentResult = withContext(Dispatchers.IO) {
db.collection("payments")
.whereEqualTo("userId", document.id)
.get()
.await()
}
for (paymentDocument in paymentResult) {
Log.d(TAG, "Payment ${paymentDocument.id}")
}
}
} catch (exception: Exception) {
Log.w(TAG, "Error getting documents.", exception)
}
}