admin管理员组文章数量:1356417
I have function like this
fun getUsersList(userIds: List<String>): List<User> {
val list = mutableListOf<User>()
userIds.forEach { id ->
getUserFromDatabase(id) { user ->
list.add(user)
}
}
return list
}
Ofcourse in this case method return empty list cos it call return list
before async tasks in cycle is completed.
So how to make it wait until cycle will be completed before return?
I learned coroutine basics but still have no idea how to handle this, that's why i'm using callbacks to update viewModel.
UPD: To clarify my situation, I get (for example) List<Type1>
from database and from this list get userId
's and after that get User
's from database. I need to build Map<Type1,User> and put it in viewModel. For that I use:
list<Type1>.forEach { type1 ->
getUserFromDatabase(type1.userId) { user ->
map.put(type1, user)
}
}
Actually question was about returning result instead of callbacks and I got the answer. Further I'll handle it myself. Thank you all.
I have function like this
fun getUsersList(userIds: List<String>): List<User> {
val list = mutableListOf<User>()
userIds.forEach { id ->
getUserFromDatabase(id) { user ->
list.add(user)
}
}
return list
}
Ofcourse in this case method return empty list cos it call return list
before async tasks in cycle is completed.
So how to make it wait until cycle will be completed before return?
I learned coroutine basics but still have no idea how to handle this, that's why i'm using callbacks to update viewModel.
UPD: To clarify my situation, I get (for example) List<Type1>
from database and from this list get userId
's and after that get User
's from database. I need to build Map<Type1,User> and put it in viewModel. For that I use:
list<Type1>.forEach { type1 ->
getUserFromDatabase(type1.userId) { user ->
map.put(type1, user)
}
}
Actually question was about returning result instead of callbacks and I got the answer. Further I'll handle it myself. Thank you all.
Share Improve this question edited Mar 28 at 18:40 Exicode asked Mar 28 at 16:59 ExicodeExicode 6110 bronze badges 3 |1 Answer
Reset to default 1Build a List<Deferred<User>>
and then call awaitAll()
on it to get the resulting List<User>
. All of that must be happening in a CoroutineScope
- in my example it is created with coroutineScope
, but you can use withContext
or some other way.
suspend fun getUsersList(userIds: List<String>): List<User> = coroutineScope {
buildList {
userIds.forEach { id ->
add(async {
getUserFromDatabase(id)
})
}
}.awaitAll()
}
本文标签: androidHow to build list from async tasks properly in KotlinStack Overflow
版权声明:本文标题:android - How to build list from async tasks properly in Kotlin - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744023349a2577610.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
SELECT * FROM users WHERE id IN (<list of ids>)
. That's more performant and directly returns aList<User>
. You won't have to bother with how to parallelize (if at all) the different calls and merge the results afterwards. You could write a new question if you need more help with the database part. – tyg Commented Mar 28 at 18:18