【发布时间】:2021-06-18 22:23:56
【问题描述】:
我正在关注 Kotlin Hands On for Kotlin/Native Concurrency here。 我无法理解最后一个示例,为什么“上面修改后的 saveToDb 函数现在处理后台调用,并且只捕获函数参数。这不会冻结父类”
class CountingModelSafer{
var count = 0
fun increment(){
count++
saveToDb(count)
}
private fun saveToDb(arg:Int) = background {
println("Doing db stuff with $arg, in main $isMainThread")
}
}
[请注意,在这些示例中,在后台,传递的 lambda 被冻结]
下面的 sn-p 导致整个 CountingModel 被冻结,但不是上面的 sn-p。有人能帮我理解为什么会这样吗?
class CountingModel{
var count = 0
fun increment(){
count++
background {
saveToDb(count)
}
}
private fun saveToDb(arg:Int){
//Do some db stuff
println("Saving $arg to db")
}
}
【问题讨论】:
标签: kotlin-multiplatform kotlin-native kotlin-multiplatform-mobile