the great warrior
03/22/2022, 8:56 PMfun checkNewItemAdded(item: PlaceItem) {
viewModelScope.launch {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
val newItemAdded = async { getPlaceItem(item.id) }
val placeItem = newItemAdded.await()
placeItem?.let {
if (!it.isChecked) {
it.isChecked = true
val checkNewItem = async { update(it) }
checkNewItem.await()
val getAllPlaces = async { getPlaces() }
val places = getAllPlaces.await()
val runningTasks = places.map { place ->
async {
val singlePlaceItem = getPlaceItem(place.id)
place to singlePlaceItem
}
}
val responses = runningTasks.awaitAll()
responses.forEach { (place, response) ->
if (response != placeItem) {
places.find { it.id == place.id }?.isChecked = false
update(place)
}
}
}
}
}
}
}
zsmb
03/22/2022, 11:15 PMasync / await
calls and just invoke all your calls manually. You only need async
for cases where you want parallel execution, which only helps in the one case where you're then doing awaitAll()
.
Otherwise, any time you have
val x = async { someOperation() }
val y = x.await()
You could replace it directly with
val y = someOperation()
zsmb
03/22/2022, 11:19 PM@Query("UPDATE places SET isChecked = false WHERE id != :placeId)
fun uncheckAllExcept(placeId: Long)
the great warrior
03/23/2022, 3:26 PM