albertgao
04/26/2018, 1:04 AMreturn
, then the statement after this return
will not be executed. But what about inside a coroutine? You can’t return
in a coroutine. Seems the answer is coroutineContext().cancel()
, But with Anko
, it said coroutineContext of type CoroutineContext can't be invoked as a function
gildor
04/26/2018, 2:15 AMcoroutineContext of type CoroutineContext can’t be invoked as a functionBecause
coroutineContext
is not a function, but property, please check docs of kotlinx.coroutinesalbertgao
04/26/2018, 2:17 AMprivate fun handleLogDetails() = launch(UI){
val result:HttpResponse = bg {
authHelper.logDetails()
}.await()
if (result.error) {
showErrorDialog(result.errorMessage)
throw CancellationException()
}
// do something when no error in result
// how to return above if I don't want to write if else
}
gildor
04/26/2018, 2:30 AMFor this, i suppose i just need to introduce real kotlin coroutine rather than the Anko one.Anko uses “real coroutines”, just update version of kotlinx.coroutines in your project
if (result.error) {
showErrorDialog(result.errorMessage)
return@launch
}
albertgao
04/26/2018, 2:37 AMimplementation "org.jetbrains.anko:anko-coroutines:$anko_version"
implementation "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"
implementation "org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version"
No kotlin coroutine here, is it fine?gildor
04/26/2018, 3:00 AMorg.jetbrains.anko:anko-coroutines
and add
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5'
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.22.5'
bg
or asReference
just copy them to your projectalbertgao
04/26/2018, 3:59 AManko-sdk25-coroutines
and anko-appcompat-v7-coroutines
? With just use the two you mentoined?
2. Every time I need to invoke some methods from activity class inside a coroutine, if my API doesn’t support cancellation which is exactly my case. I should use the asReference()
as a bridge to invoke activity methods to avoid mem leak, right?gildor
04/26/2018, 4:26 AM