Dmytro Danylyk
04/27/2018, 1:22 PMDmytro Danylyk
04/28/2018, 7:12 AMval job = Job()
fun loadData() = job.launch(UI) {
val result = withContext(CommonPool) { ... }
view.showResult(result)
}
Dmytro Danylyk
04/28/2018, 7:15 AMasync
require job receiver?
val job = Job()
fun loadData() = job.launch(UI) {
// val result = async(CommonPool) { ... }.await() can't do that, async require job receiver
val result = job.async(CommonPool) { ... }.await() // this one is correct?
view.showResult(result)
}
Vsevolod Tolstopyatov [JB]
04/28/2018, 8:25 AMe.g. my typical android code would look like thisEmpty jobs are not recommended, it doesn’t really make sense. Job should be bond with either your Android activity or another job.
But, if I want to launch another coroutine how I can achieve that
async
builder itself will have job (or something similar to it) as receiver, so nested async will work as expected:
val loadData() = job.launch(UI) {
val innerTask = async(CommonPool) {}
// Here inner task implicitly inherits job of UI activity and will be cancelled when activity is destroyed
}
Dmytro Danylyk
04/28/2018, 9:51 AMEmpty jobs are not recommended, it doesn’t really make sense.I have made empty job to cancel it in activity
onDestroy
function. (because lifecycle aware stuff is available from library only, it’s not part of android sdk). With lifecycle callback it will look like in your sample lifecycle.job().launch(UI)
.Dmytro Danylyk
04/28/2018, 9:52 AMwithContext
will stay the same correct?Vsevolod Tolstopyatov [JB]
04/28/2018, 9:59 AMwithContext
will stay correct, but this part of design is not yet properly investigated. Probably withContext
should have job as wellDmytro Danylyk
04/28/2018, 10:05 AMwithContext
need job as well, it would require write more code. E.g. currently in http client
fun loadUser(id: String,
context: CoroutineContext = CommonPool) = withContext(context) {
// logic
}
With new design:
fun loadUser(id: String, context: CoroutineContext = CommonPool, job: Job)
= job.withContext(context) {
// logic
}
I will have to pass job everywhere, am I right?Vsevolod Tolstopyatov [JB]
04/28/2018, 10:48 AMEternal
.
fun loadUser(id: String, context: CoroutineContext) = Eternal.withContext(context) {
// logic
}
Vsevolod Tolstopyatov [JB]
04/28/2018, 11:19 AMdave08
04/29/2018, 4:56 AM