Kotlin Coroutines the right way in Android

前端 未结 9 1095
醉酒成梦
醉酒成梦 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 06:56

    After struggling with this question for days, I think the most simple and clear async-await pattern for Android activities using Kotlin is:

    override fun onCreate(savedInstanceState: Bundle?) {
        //...
        loadDataAsync(); //"Fire-and-forget"
    }
    
    fun loadDataAsync() = async(UI) {
        try {
            //Turn on busy indicator.
            val job = async(CommonPool) {
               //We're on a background thread here.
               //Execute blocking calls, such as retrofit call.execute().body() + caching.
            }
            job.await();
            //We're back on the main thread here.
            //Update UI controls such as RecyclerView adapter data.
        } 
        catch (e: Exception) {
        }
        finally {
            //Turn off busy indicator.
        }
    }
    

    The only Gradle dependencies for coroutines are: kotlin-stdlib-jre7, kotlinx-coroutines-android.

    Note: Use job.await() instead of job.join() because await() rethrows exceptions, but join() does not. If you use join() you will need to check job.isCompletedExceptionally after the job completes.

    To start concurrent retrofit calls, you can do this:

    val jobA = async(CommonPool) { /* Blocking call A */ };
    val jobB = async(CommonPool) { /* Blocking call B */ };
    jobA.await();
    jobB.await();
    

    Or:

    val jobs = arrayListOf>();
    jobs += async(CommonPool) { /* Blocking call A */ };
    jobs += async(CommonPool) { /* Blocking call B */ };
    jobs.forEach { it.await(); };
    

提交回复
热议问题