使用属性作为 Kotlin 协程的访问器

jes*_*.tt 1 android kotlin fusedlocationproviderapi kotlin-coroutines

Kotlin 协程问题...正在努力使用属性而不是函数作为异步调用的访问器。

背景是我正在尝试将FusedLocationProviderClientkotlinx-coroutines-play-services库一起使用,以便使用该.await()方法Task而不是添加回调......

目前有一个属性 getter 被踢出挂起函数,但不确定如何正确启动协程以避免

找到所需单位 XYZ

错误...

 val lastUserLatLng: LatLng?
        get() {
            val location = lastUserLocation
            return if (location != null) {
                LatLng(location.latitude, location.longitude)
            } else {
                null
            }
        }

    val lastUserLocation: Location?
        get() {
            GlobalScope.launch {
                return@launch getLastUserLocationAsync()  <--- ERROR HERE
            }
        }

    private suspend fun getLastUserLocationAsync() : Location? = withContext(Dispatchers.Main) {
        return@withContext if (enabled) fusedLocationClient.lastLocation.await() else null
    }
Run Code Online (Sandbox Code Playgroud)

关于如何处理这个问题有什么想法吗?

tyn*_*ynn 5

属性不能是异步的。一般来说,您不应该同步异步调用。当您需要一个值时,您必须返回Deferred并调用它。await()

val lastUserLatLng: Deferredd<LatLng?>
    get() = GlobalScope.async {
        lastUserLocation.await()?.run {
            LatLng(latitude, longitude)
        }
    }

val lastUserLocation: Deferred<Location?>
    get() = GlobalScope.async {
        getLastUserLocationAsync()
    }

private suspend fun getLastUserLocationAsync() : Location? = withContext(Dispatchers.Main) {
    return@withContext if (enabled) fusedLocationClient.lastLocation.await() else null
}
Run Code Online (Sandbox Code Playgroud)

但从技术上讲这是可能的,尽管你应该这样做。runBlocking()阻塞直到有一个值可用并返回它。