Kotlin Coroutines the right way in Android

前端 未结 9 1116
醉酒成梦
醉酒成梦 2020-12-02 06:51

I\'m trying to update a list inside the adapter using async, I can see there is too much boilerplate.

Is it the right way to use Kotlin Coroutines?

can this

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 07:00

    If you want to return some thing from background thread use async

    launch(UI) {
       val result = async(CommonPool) {
          //do long running operation   
       }.await()
       //do stuff on UI thread
       view.setText(result)
    }
    

    If background thread is not returning anything

    launch(UI) {
       launch(CommonPool) {
          //do long running operation   
       }.await()
       //do stuff on UI thread
    }
    

提交回复
热议问题