I would like to suspend a kotlin coroutine until a method is called from outside, just like the old Java object.wait() and object.notify() methods. How do I do that?
这里:Correctly implementing wait and notify in Kotlin是如何使用Kotlin线程(阻塞)实现这一点的答案.这里:Suspend coroutine until condition is true是如何使用CompleteableDeferreds完成此操作的答案,但我不希望每次都必须创建CompleteableDeferred的新实例.
我目前正在这样做:
var nextIndex = 0
fun handleNext(): Boolean {
if (nextIndex < apps.size) {
//Do the actual work on apps[nextIndex]
nextIndex++
}
//only execute again if nextIndex is a valid index
return nextIndex < apps.size
}
handleNext()
// The returned function will be called multiple times, which I would like to replace with something like notify()
return ::handleNext