【发布时间】:2019-10-01 13:39:12
【问题描述】:
Kotlin Coroutines 问题...挣扎着使用属性而不是函数作为异步调用的访问器。
背景是我正在尝试将FusedLocationProviderClient 与kotlinx-coroutines-play-services 库一起使用,以便在Task 上使用.await() 方法而不是添加回调...
目前有一个属性 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
}
关于如何处理这个问题有什么想法吗?
【问题讨论】:
-
使用异步而不是启动
标签: android kotlin kotlin-coroutines fusedlocationproviderapi