小编KTC*_*TCO的帖子

Kotlin Coroutines在Android中正确的方式

我正在尝试使用异步更新适配器内的列表,我可以看到有太多的样板.

这是使用Kotlin Coroutines的正确方法吗?

这可以更优化吗?

fun loadListOfMediaInAsync() = async(CommonPool) {
        try {
            //Long running task 
            adapter.listOfMediaItems.addAll(resources.getAllTracks())
            runOnUiThread {
                adapter.notifyDataSetChanged()
                progress.dismiss()
            }
        } catch (e: Exception) {
            e.printStackTrace()
            runOnUiThread {progress.dismiss()}
        } catch (o: OutOfMemoryError) {
            o.printStackTrace()
            runOnUiThread {progress.dismiss()}
        }
    }
Run Code Online (Sandbox Code Playgroud)

android coroutine async-await kotlin

38
推荐指数
4
解决办法
3万
查看次数

标签 统计

android ×1

async-await ×1

coroutine ×1

kotlin ×1