如何等待暂停函数在Kotlin中执行其他代码之前完成
我想知道我是否可以等到暂停功能完成之前完成其他代码? loadParticleswithOutSetCall
,我在setparticlepicking
的内部称呼,具有悬挂函数。我不希望调用setParticlePicking
中的其他任何内容,直到悬挂功能完成为止。请告诉我。
fun setParticlePicking(particlePicking: ParticlePicking) {
loadParticlesWithoutSetCall()
manualParticleMarkers?.forEach {
imageContainerElem.remove(it)
}
currentParticlePicking = particlePicking
manualParticleMarkers = (particlePicking.pickings [imageID]?.map {
val marker = ManualParticleMarker(it.x, it.y, image = imageElem, trueHeight = particlesDat!!.imageHeight, trueWidth = particlesDat!!.imageWidth, h = particlesDat!!.h(0), w = particlesDat!!.w(0), parentElem = [email protected])
imageContainerElem.add(marker)
marker
}?: mutableListOf()) as MutableList<ManualParticleMarker>
placeMarkers()
}
fun loadParticlesWithoutSetCall() {
AppScope.launch {
// clear everything
particlesElem.removeAll()
// load the particles
val loadingElem = particlesElem.loading("Fetching particles ...")
val particles: ParticlesData? = try {
loader()
} catch (t: Throwable) {
errorMessage(t)
null
} finally {
particlesElem.remove(loadingElem)
toWait = true
}
particlesDat = particles
}
}
I'm wondering if I can wait until a suspend function has completed before executing other code? loadParticlesWithoutSetCall
, which I call inside of setParticlePicking
, has a suspend function. I do not want anything else in setParticlePicking
to be called until the suspend function has finished. Please let me know.
fun setParticlePicking(particlePicking: ParticlePicking) {
loadParticlesWithoutSetCall()
manualParticleMarkers?.forEach {
imageContainerElem.remove(it)
}
currentParticlePicking = particlePicking
manualParticleMarkers = (particlePicking.pickings [imageID]?.map {
val marker = ManualParticleMarker(it.x, it.y, image = imageElem, trueHeight = particlesDat!!.imageHeight, trueWidth = particlesDat!!.imageWidth, h = particlesDat!!.h(0), w = particlesDat!!.w(0), parentElem = [email protected])
imageContainerElem.add(marker)
marker
}?: mutableListOf()) as MutableList<ManualParticleMarker>
placeMarkers()
}
fun loadParticlesWithoutSetCall() {
AppScope.launch {
// clear everything
particlesElem.removeAll()
// load the particles
val loadingElem = particlesElem.loading("Fetching particles ...")
val particles: ParticlesData? = try {
loader()
} catch (t: Throwable) {
errorMessage(t)
null
} finally {
particlesElem.remove(loadingElem)
toWait = true
}
particlesDat = particles
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过在
fun
之前添加暂停
修饰符,标记两个功能暂停功能。然后移动appscope.launch {}
呼叫以将调用打到setParticlePicking
,无论它在哪里(您未包含在示例中)。It should look something like this:
Since you will have removed the
AppScope.launch {}
call fromloadParticlesWithoutSetCall
,setParticlePicking
will wait for its code to在使用其余代码之前完成之前,因为悬挂功能为默认情况下顺序。Mark both functions suspending functions by adding the
suspend
modifier beforefun
. Then move theAppScope.launch {}
call to wrap the call tosetParticlePicking
, wherever it is (you did not include in the sample).It should look something like this:
Since you will have removed the
AppScope.launch {}
call fromloadParticlesWithoutSetCall
,setParticlePicking
will wait for its code to complete before moving on with the rest of the code, because suspending functions are sequential by default.