https://kotlinlang.org logo
#coroutines
Title
# coroutines
k

Kulwinder Singh

11/08/2019, 8:12 AM
I have
LiveData<List<User>>
loaded from firestore now i want to load `Appointment`s for each user from firestore, in end i want to create something like this
Map<User,Appointment>
. which
flow
operator can helps me in this situation, or flow is not best suited for this?
z

Zach Klippenstein (he/him) [MOD]

11/08/2019, 6:00 PM
You could use flow for this, but I'm not sure you'd need to. Once you have your
List<User>
you can just do something like:
Copy code
users
  .map { user ->
    user to async { loadAppointments(user) }
  } // List<Pair<User, Deferred<Appointment>>>
  .toMap() // Map<User, Deferred<Appointment>>
  // This is an inline operator so it's allowed to make suspending calls if you're already in a suspend function.
  .mapValues { it.value.await() } // Map<User, Appointment>
2 Views