如何等待暂停函数在Kotlin中执行其他代码之前完成

发布于 2025-02-11 11:19:10 字数 1563 浏览 1 评论 0原文

我想知道我是否可以等到暂停功能完成之前完成其他代码? 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪异←思 2025-02-18 11:19:10

通过在fun之前添加暂停修饰符,标记两个功能暂停功能。然后移动appscope.launch {}呼叫以将调用打到setParticlePicking,无论它在哪里(您未包含在示例中)。

It should look something like this:

AppScope.launch {
    setParticlePicking(particlePicking)
}

Since you will have removed the AppScope.launch {} call from loadParticlesWithoutSetCall, setParticlePicking will wait for its code to在使用其余代码之前完成之前,因为悬挂功能为默认情况下顺序

Mark both functions suspending functions by adding the suspend modifier before fun. Then move the AppScope.launch {} call to wrap the call to setParticlePicking, wherever it is (you did not include in the sample).

It should look something like this:

AppScope.launch {
    setParticlePicking(particlePicking)
}

Since you will have removed the AppScope.launch {} call from loadParticlesWithoutSetCall, setParticlePicking will wait for its code to complete before moving on with the rest of the code, because suspending functions are sequential by default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文