【发布时间】:2019-07-10 15:25:54
【问题描述】:
如果条件不满足,我需要退出 kotlin 中的协程。我想避免使用嵌套条件来保持我的代码干净。这就是我所拥有的:
GlobalScope.launch {
var condition: Boolean = false
if (!condition) {
//this does nothing
this.cancel()
}
println("I shouldn't print")
}
【问题讨论】:
-
只需使用
return语句。 -
是协程作用域,不是函数
-
这是一个 lambda。您可以使用
return@launch从它返回。 -
看来我需要访问文档。有效! .将其发布为答案