kotlin - 此处不允许“返回”:Kotlin Coroutine 启动(UI)块
问题描述
fun onYesClicked(view: View) {
launch(UI) {
val res = post(context!!,"deleteRepo")
if(res!=null){
fetchCatalog(context!!)
catalogActivityCatalog?.refresh()
}
}
}
上面的代码工作正常。我想通过返回(从而停止执行) if 来避免 if 中的嵌套部分res == null
,就像这样,
fun onYesClicked(view: View) {
launch(UI) {
val res = post(context!!,"deleteRepo")
if(res==null)return //this line changed <---A
fetchCatalog(context!!) //moved outside if block
catalogActivityCatalog?.refresh() //moved outside if block
}
}
当我在 <--A 指示的行中使用 return 时,它说这里不允许“return”
这里有退出launch
块的关键字吗?什么是可以在这里使用而不是返回的替代方法?
解决方案
必须使用指定返回的目的地return@...
fun onYesClicked(view: View) {
launch(UI) {
val res = post(context!!,"deleteRepo")
if(res==null)return@launch //return at launch
fetchCatalog(context!!)
catalogActivityCatalog?.refresh()
}
}
推荐阅读
- python - “找不到任何有价值的窗口提供程序”错误
- python - 如何进一步优化这段代码?
- lstm - 如何使用二进制标签计算 LSTM 的累积分数?
- wso2dss - WSO2EI DSS 中未发现 ERP 操作错误
- swift - GKState:为什么 StateMachine == nil 代表调用?
- php - 我想通过比较两个索引从嵌套关联数组中删除
- c# - 在 ListBoxItem DataTemplate 内的嵌套控件中处理相同的单击事件 - WPF
- c# - Listview 行工具提示显示持续时间
- mysql - 如何对计数数据进行分组
- python-3.x - 如何修复 Python 中的“值错误”(使用情节)