gpt4 book ai didi

android - Kotlin 协程 java.lang.IllegalStateException : Task is not yet complete even though task returns a value

转载 作者:行者123 更新时间:2023-11-30 05:03:25 39 4
gpt4 key购买 nike

我正在尝试使用 await() 从 firebase 检索数据,当我尝试在没有结果包装器的情况下执行此操作时,代码有效,但同样的事情崩溃了:“java.lang.IllegalStateException:任务尚未完成”

下一个代码崩溃

  suspend fun isUserRegisteredOnServer(): Result<Exception, Boolean> =
try {
val result = userRef.get().await().exists()
Result.build { result }
}
catch (e : Exception) {
Result.Error(e)
}

以下没有

suspend fun tempIsRegistered() : Boolean
{
return userRef.get().await().exists()
}

结果类:

sealed class Result <out E,out V > {

data class Value<out V>(val value : V) : Result<Nothing, V>()
data class Error<out E>(val error : E) : Result<E, Nothing>()

companion object Factory
{
inline fun <V> build(function : () -> V): Result<Exception, V> =
try {
Value(function.invoke())
}catch (e: Exception) {
Error(e)
}
}
}

同样值得注意的是,我正在调用这些函数

CoroutineScope(IO).launch {}

堆栈跟踪:

2019-09-05 18:50:54.121 23507-23561/E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: , PID: 23507
java.lang.IllegalStateException: Task is not yet complete
at com.google.android.gms.common.internal.Preconditions.checkState(Unknown Source:29)
at com.google.android.gms.tasks.zzu.zzb(Unknown Source:121)
at com.google.android.gms.tasks.zzu.getResult(Unknown Source:12)
at firebase.database.FirestoreDatabaseRepository.fetchUserLists(FirestoreDatabaseRepository.kt:79)
at com.mainfragment.MainFragmentViewModel$handleEvent$1.invokeSuspend(MainFragmentViewModel.kt:74)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)

最佳答案

继续使用任务 API:

try {
val result = Tasks.await(userRef.get(), 2, TimeUnit.SECONDS)
Result.build { result }
}
catch (e : Exception) {
Result.Error(e)
}

关于android - Kotlin 协程 java.lang.IllegalStateException : Task is not yet complete even though task returns a value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57809396/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com