将Firebase与Kotlin协程一起使用
我在我的应用中使用Kotlin协程,并选择了Firebase作为数据库和存储的选择. 探索了Firebase之后,我意识到它的所有API都是异步的,并且异步调用的结果都在回调中返回,而摆脱回调是我在应用中使用Kotlin协程的主要原因.
I am using Kotlin coroutines in my app and have chosen firebase as my choice for database and storage. After exploring firebase I realized that all its APIs are asynchronous and the result of the asynchronous calls are returned in a callback, and getting rid of callbacks is the main reason why I am using Kotlin coroutines in my app.
这是我编写的用于将文件上传到Firebase云存储的代码,但它给出了任务尚未完成"错误.
This is the code I have written to upload a file to firebase cloud storage but it is giving "Task is not yet complete" error.
private suspend fun saveImage(filePath: String): String? {
val storage = FirebaseStorage.getInstance("gs://myapp-9a648.appspot.com/")
val storageRef = storage.reference
val file = Uri.fromFile(File(filePath))
val imageRef = storageRef.child("images/${file.lastPathSegment}")
return withContext(Dispatchers.IO) {
imageRef.putFile(file).snapshot.storage.downloadUrl.result.toString()
}
}
E/Android运行时:致命异常:主要 程序:pk.com.kotlinapp,PID:7491 java.lang.IllegalStateException:任务尚未完成 在com.google.android.gms.common.internal.Preconditions.checkState(未知 来源) 在com.google.android.gms.tasks.zzu.zzb(未知来源) 位于com.google.android.gms.tasks.zzu.getResult(未知来源) 在prk.com.kotlinapptest.DatabaseManager $ saveImage $ 2.invokeSuspend(DatabaseManager.kt:28) 在kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 在kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241) 在kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594) 在kotlinx.coroutines.scheduling.CoroutineScheduler.access $ runSafely(CoroutineScheduler.kt:60) 在kotlinx.coroutines.scheduling.CoroutineScheduler $ Worker.run(CoroutineScheduler.kt:740)
E/AndroidRuntime: FATAL EXCEPTION: main Process: pk.com.kotlinapp, PID: 7491 java.lang.IllegalStateException: Task is not yet complete at com.google.android.gms.common.internal.Preconditions.checkState(Unknown Source) at com.google.android.gms.tasks.zzu.zzb(Unknown Source) at com.google.android.gms.tasks.zzu.getResult(Unknown Source) at prk.com.kotlinapptest.DatabaseManager$saveImage$2.invokeSuspend(DatabaseManager.kt:28) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241) 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:740)
有什么方法可以将文件上传到Firebase云存储并获取下载URL,而无需在其成功回调中获取下载URL?
Is there any way I can upload a file to firebase cloud storage and get back the download URL without getting the download URL in its success callback?
kotlinx-coroutines-play-services
await 扩展功能,用于等待任务完成,例如:
kotlinx-coroutines-play-services
library provides await extension function that allows to await the task completion, like:
...
dependencies {
...
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.1"
}
return withContext(Dispatchers.IO) {
imageRef
.putFile(file)
.await() // await() instead of snapshot
.storage
.downloadUrl
.await() // await the url
.toString()
}