观察者内部协程内部的地理编码未运行

Ste*_*ail 5 geocoding kotlin android-studio kotlin-coroutines

在我的片段中,我有一个bindUI()方法,其中包含我正在观察的一些 liveData,并且在 lambda 内部,我想使用该值location(包含纬度和经度值)来根据它包含的坐标检索地址列表。有人告诉我地理编码应该在协程内部完成,所以我正在尝试这样做,但launch协程内部的代码似乎没有运行。我已经放置了日志语句,但它们没有运行,并且似乎没有结果打印在我的文本视图上。调试器还说,当我在其上放置断点时,没有可运行的代码。我在这里搞砸了什么?

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
            }
        })
    }
Run Code Online (Sandbox Code Playgroud)

有趣的是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()
}
Run Code Online (Sandbox Code Playgroud)