You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin协程中continuation调用invokeOnCancellation/invokeOnCompletion报未解析引用问题求助

Kotlin协程中continuation调用invokeOnCancellation/invokeOnCompletion报未解析引用问题求助

各位大佬好,我还在学习Kotlin的路上,麻烦大家多包涵🙏

最近写代码的时候遇到个头疼的问题:调用continuation.invokeOnCancellation时一直提示“unresolved reference invokeOnCancellation”,换成continuation.invokeOnCompletion也还是报同样的未解析引用错误。

我的代码如下:

suspend fun getEntireJobMapFromFirebase() = suspendCoroutine<Unit> { continuation ->

    val database = Firebase.database
    val job = database.getReference("jobs")

    val listener = object : ValueEventListener {

        override fun onDataChange(dataSnapshot: DataSnapshot) {
            // This method is called whenever data at this location is updated.
            Log.d(jobMapTag, "job map value from firebase is: $dataSnapshot")
            commonViewModel.updateJobCategoryList(dataSnapshot)
        }

        override fun onCancelled(error: DatabaseError) {
            // Failed to read value
            Log.w(jobMapTag, "Failed to read value.", error.toException())
            continuation.resumeWithException(error.toException())
        }
    }

    job.addValueEventListener(listener)
    continuation.invokeOnCancellation { job.removeEventListener(listener) }
    // remove the listener when the coroutine is cancelled
}

我已经引入了协程核心依赖:implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4',但还是解决不了这个问题,有没有大佬能帮忙看看哪里出问题了?谢谢大家!

备注:内容来源于stack exchange,提问作者Lazza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 16:02:58