Android studio 在观察者内部的协同程序内部进行地理编码是';t运行
在我的片段中,我有一个Android studio 在观察者内部的协同程序内部进行地理编码是';t运行,android-studio,kotlin,geocoding,kotlin-coroutines,Android Studio,Kotlin,Geocoding,Kotlin Coroutines,在我的片段中,我有一个bindUI()方法,其中包含一些我正在观察的liveData,在lambda内部,我想使用值location(其中包含一个纬度和经度值)根据它包含的坐标检索地址列表。我被告知地理编码应该在协同程序内完成,所以我正在尝试这样做,但是launch协同程序内的代码似乎没有运行。我已经放置了日志语句,但它们不运行,并且在我的textView上似乎没有打印任何结果。当我在调试器上放置断点时,调试器还说没有可运行的代码。我搞砸了什么 private fun bindUI() = la
bindUI()
方法,其中包含一些我正在观察的liveData,在lambda内部,我想使用值location
(其中包含一个纬度和经度值)根据它包含的坐标检索地址列表。我被告知地理编码应该在协同程序内完成,所以我正在尝试这样做,但是launch
协同程序内的代码似乎没有运行。我已经放置了日志语句,但它们不运行,并且在我的textView上似乎没有打印任何结果。当我在调试器上放置断点时,调试器还说没有可运行的代码。我搞砸了什么
private fun bindUI() = launch(Dispatchers.Main) {
// fetch the location
val weatherLocation = viewModel.weatherLocation.await()
// Observe the location for changes
weatherLocation.observe(viewLifecycleOwner, Observer { location ->
if (location == null) return@Observer
//TODO:sp update the location text view
launch(Dispatchers.Main) {
val task = updateLocation(location)
locationTxtView.text = task[0].countryName
}
})
}
下面是updateLocation()
的乐趣:
private suspend fun updateLocation(location: WeatherLocation): MutableList<Address> {
return async(Dispatchers.IO) {
val geocoder = Geocoder(activity, Locale.getDefault())
val addr = geocoder.getFromLocation(location.latitude, location.longitude, 10)
return@async addr
}.await()
}
private suspend fun updateLocation(位置:WeatherLocation):可变列表{
返回异步(Dispatchers.IO){
val geocoder=geocoder(活动,Locale.getDefault())
val addr=geocoder.getFromLocation(location.latitude,location.longitude,10)
return@async地址
}.等待
}