oday
08/31/2022, 6:46 PMviewModelScope.launch(<http://dispatchers.io|dispatchers.io>) {
val filterSources = try {
listOf(
launch { getLocation.execute() },
launch { getCategory.execute() },
launch { getPeriod.execute() },
launch { getDateRange.execute() }
).joinAll().
} catch (e: Throwable) {
}
}
but unable to figure out how to get that to a map where i can use the stuff insideFrancesc
08/31/2022, 6:49 PMasync
and then awaitAll
on the listoday
08/31/2022, 6:50 PModay
08/31/2022, 6:50 PModay
08/31/2022, 7:29 PModay
08/31/2022, 7:29 PMFrancesc
08/31/2022, 7:32 PMawaitAll
returns a List with the result of your tasks. However, if your tasks return different types, then you'll just get a list of Any
- in this case, don't make a list and launch the async
individually and then wait for themoday
08/31/2022, 7:32 PModay
08/31/2022, 7:32 PMFrancesc
08/31/2022, 7:33 PModay
08/31/2022, 8:09 PModay
08/31/2022, 8:10 PMval tasks = listOf(
async { getLocation.execute() },
async { getCategory.execute() },
async { getPeriod.execute() },
async { getDateRange.execute() }
)
val location = (tasks[0].await() as Optional<GetLocation.Location>).orNull()
this location is a GetLocation.Result.Success
and it has a member inside called location
, THAT Member is what i want to cast, how do I get that member out of tasks[0].await()
?oday
08/31/2022, 8:11 PMlocation
inside the Success is the one that’s Optional, not the parent Success objectFrancesc
08/31/2022, 8:12 PMval locationDeferred = async { getLocation.execute() },
val categoryDeferred = async { getCategory.execute() },
// others
val location = locationDeffered.await()
val category = categoryDeferred.await()
// await others
oday
08/31/2022, 8:13 PMFrancesc
08/31/2022, 8:13 PModay
08/31/2022, 8:14 PMFrancesc
08/31/2022, 8:14 PMawait
the deferred, you won't get any result from it - it runs as soon as you launch it, but any result of error is stored until you call awaitoday
08/31/2022, 8:14 PMFrancesc
08/31/2022, 8:15 PMgetLocation.execute()
?oday
08/31/2022, 8:15 PModay
08/31/2022, 8:17 PModay
08/31/2022, 8:17 PModay
08/31/2022, 8:18 PMFrancesc
08/31/2022, 8:18 PModay
08/31/2022, 8:19 PMResult
class (with Success and Error) I can’t simply cast that entire thing into the type of Success’ member variable in 1 gooday
08/31/2022, 8:20 PMFrancesc
08/31/2022, 8:20 PMResult
class might have a fold
or similar that you can use to get the value or erroroday
08/31/2022, 8:21 PModay
08/31/2022, 8:21 PModay
08/31/2022, 8:21 PMFrancesc
08/31/2022, 8:23 PMsealed interface Result<out T> {
class Success<T>(val value: T) : Result<T>
class Failure(val error: Throwable) : Result<Nothing>
}
inline fun <R, T> Result<T>.fold(
onSuccess: (value: T) -> R,
onFailure: (exception: Throwable) -> R,
): R = when (this) {
is Result.Success -> onSuccess(value)
is Result.Failure -> onFailure(error)
}
and you would use it like so
repoResponse.fold(
onSuccess = { response ->
// handle successful response
},
onFailure = { error ->
// handle error
}
)
oday
08/31/2022, 8:25 PModay
08/31/2022, 8:25 PModay
08/31/2022, 8:25 PModay
09/01/2022, 12:57 PMviewModelScope.launch(<http://dispatchers.io|dispatchers.io>) {
val locationDeferred = async { getLocation.execute() }
val categoryDeferred = async { getCategory.execute() }
val periodDeferred = async { getPeriod.execute() }
val dateRangeDeferred = async { getDateRange.execute() }
var location : GetLocation.Location? = null
var dateRange : DateRange? = null
lateinit var category : Category
lateinit var period : Period
locationDeferred.await().fold(
onSuccess = { response ->
location = response.orNull()
},
onFailure = {
logger.log(it)
}
)
categoryDeferred.await().fold(
onSuccess = { response ->
category = response
},
onFailure = {
logger.log(it)
}
)
but when coming back to this code it doesnt trigger againoday
09/01/2022, 12:57 PM