如何使用Kotlin协程处理回调 [英] how to handle callback using kotlin coroutines
问题描述
以下代码段在顺序代码流中将结果返回为"null".我知道协程可能是异步处理回调的可行解决方案.
the following snippet returns the result as 'null' on sequential code flow. I understand coroutines could be a viable solution to handle the callback asynchronously.
fun getUserProperty(path: String): String? {
var result: String? = null
database.child(KEY_USERS).child(getUid()).child(path)
.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(error: DatabaseError) {
Log.e(TAG, "error: $error")
}
override fun onDataChange(snapshot: DataSnapshot) {
Log.w(TAG, "value: ${snapshot.value}")
result = snapshot.value.toString()
}
})
return result
}
协程在这种情况下可以等到回调(onDataChange()/onCancelled())的结果吗?
Can the coroutines be of any help in this scenario to wait until the result of the callbacks (onDataChange()/onCancelled())?
推荐答案
由于Firebase Realtime Database SDK不提供任何暂停功能,因此协程在处理其API时没有帮助.您需要将回调转换为暂停函数,以便能够在协程中等待结果.
Since the Firebase Realtime Database SDK doesn't provide any suspend functions, coroutines are not helpful when dealing with its APIs. You would need to convert the callback into a suspend function in order for you to be able to await the result in a coroutine.
这是一个可以执行此操作的暂停扩展功能(我通过谷歌搜索发现了一个解决方案搜索):
Here's a suspend extension function that does this (I discovered a solution it by doing a google search):
suspend fun DatabaseReference.getValue(): DataSnapshot {
return async(CommonPool) {
suspendCoroutine<DataSnapshot> { continuation ->
addListenerForSingleValueEvent(FValueEventListener(
onDataChange = { continuation.resume(it) },
onError = { continuation.resumeWithException(it.toException()) }
))
}
}.await()
}
class FValueEventListener(val onDataChange: (DataSnapshot) -> Unit, val onError: (DatabaseError) -> Unit) : ValueEventListener {
override fun onDataChange(data: DataSnapshot) = onDataChange.invoke(data)
override fun onCancelled(error: DatabaseError) = onError.invoke(error)
}
有了这个,您现在如何在协程中等待getValue()
在DatabaseReference上的可疑方法.
With this, you now how a getValue()
suspect method on DatabaseReference that can be awaited in a coroutine.
这篇关于如何使用Kotlin协程处理回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!