AsyncTask as kotlin coroutine

£可爱£侵袭症+ 提交于 2020-04-29 18:44:22

问题


Typical use for AsyncTask: I want to run a task in another thread and after that task is done, I want to perform some operation in my UI thread, namely hiding a progress bar.

The task is to be started in TextureView.SurfaceTextureListener.onSurfaceTextureAvailable and after it finished I want to hide the progress bar. Doing this synchronously does not work because it would block the thread building the UI, leaving the screen black, not even showing the progress bar I want to hide afterwards.

So far I use this:

inner class MyTask : AsyncTask<ProgressBar, Void, ProgressBar>() {
    override fun doInBackground(vararg params: ProgressBar?) : ProgressBar {
        // do async
        return params[0]!!
    }

    override fun onPostExecute(result: ProgressBar?) {
        super.onPostExecute(result)
        result?.visibility = View.GONE
    }
}

But these classes are beyond ugly so I'd like to get rid of them. I'd like to do this with kotlin coroutines. I've tried some variants but none of them seem to work. The one I would most likely suspect to work is this:

runBlocking {
        // do async
}
progressBar.visibility = View.GONE

But this does not work properly. As I understand it, the runBlockingdoes not start a new thread, as AsyncTask would, which is what I need it to do. But using the thread coroutine, I don't see a reasonable way to get notified when it finished. Also, I can't put progressBar.visibility = View.GONE in a new thread either, because only the UI thread is allowed to make such operations.

Im really new to coroutines so I don't quite understand what I'm missing here.


回答1:


First, you have to run coroutine with launch(context), not with runBlocking: https://kotlinlang.org/docs/reference/coroutines/coroutine-context-and-dispatchers.html

Second, to get the effect of onPostExecute, you have to use

Activity.runOnUiThread(Runnable) or View.post(Runnable).




回答2:


To use a coroutine you need a couple of things:

  • Implement CoroutineScope interface.
  • References to Job and CoroutineContext instances.
  • Use suspend function modifier to suspend a coroutine without blocking the Main Thread when calling function that runs code in Background Thread.
  • Use withContext(Dispatchers.IO) function to run code in background thread and launch function to start a coroutine.

Usually I use a separate class for that, e.g. "Presenter" or "ViewModel" depends on architecture I'm using:

class Presenter : CoroutineScope {
    private var job: Job = Job()
    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + job // to run code in Main(UI) Thread

    // call this method to cancel a coroutine when you don't need it anymore,
    // e.g. when user closes the screen
    fun cancel() {
        job.cancel()
    }

    fun startTask() = launch {
        val result = doSomeBackgroundWork() // runs in background thread without blocking the Main Thread
        doUiStuff(result)
    }

    private suspend fun doSomeBackgroundWork(): String = withContext(Dispatchers.IO) { // to run code in Background Thread
        // do async work
        delay(1000) // simulate async work
        return@withContext "SomeResult"
    }

    // Runs in Main(UI) Thread
    private fun doUiStuff(result: String) {
        // hide progress
    }

}



回答3:


You can get ProgressBar to run on the UI Main Thread, while using coroutine to run your task asynchronously.

Inside your override fun onCreate() method,

GlobalScope.launch(Dispatchers.Main) { // Coroutine Dispatcher confined to Main UI Thread
    yourTask() // your task implementation
}

You can initialize,

private var jobStart: Job? = null

In Kotlin, var declaration means the property is mutable. If you declare it as val, it is immutable, read-only & cannot be reassigned.

Outside the onCreate() method, yourTask() can be implemented as a suspending function, which does not block main caller thread.

When the function is suspended while waiting for the result to be returned, its running thread is unblocked for other functions to execute.

private suspend fun yourTask() = withContext(Dispatchers.Default){ // with a given coroutine context
    jobStart = launch {
       try{
        // your task implementation
       } catch (e: Exception) {
             throw RuntimeException("To catch any exception thrown for yourTask", e)
      }
    }
  }

For your progress bar, you can create a button to show the progress bar when the button is clicked.

buttonRecognize!!.setOnClickListener {
    trackProgress(false)
}

Outside of onCreate(),

private fun trackProgress(isCompleted:Boolean) {
    buttonRecognize?.isEnabled = isCompleted // ?. safe call
    buttonRecognize!!.isEnabled // !! non-null asserted call

    if(isCompleted) {
        loading_progress_bar.visibility = View.GONE
    } else {
        loading_progress_bar.visibility = View.VISIBLE
    }
}

An additional tip is to check that your coroutine is indeed running on another thread, eg. DefaultDispatcher-worker-1,

Log.e("yourTask", "Running on thread ${Thread.currentThread().name}")

Hope this is helpful.



来源:https://stackoverflow.com/questions/55208748/asynctask-as-kotlin-coroutine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!