AsyncTask 作为 kotlin 协程 [英] AsyncTask as kotlin coroutine

查看:53
本文介绍了AsyncTask 作为 kotlin 协程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AsyncTask 的典型用途:我想在另一个线程中运行一个任务,该任务完成后,我想在我的 UI 线程中执行一些操作,即隐藏进度条.

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.

任务将在 TextureView.SurfaceTextureListener.onSurfaceTextureAvailable 中开始,完成后我想隐藏进度条.同步执行此操作不起作用,因为它会阻止构建 UI 的线程,使屏幕变黑,甚至不显示我之后想隐藏的进度条.

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
    }
}

但是这些类太丑了,所以我想摆脱它们.我想用 kotlin 协程来做到这一点.我尝试了一些变体,但它们似乎都不起作用.我最有可能怀疑的工作是这样的:

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

但这不能正常工作.据我了解,runBlocking 不会像 AsyncTask 那样启动一个新线程,而这正是我需要它做的.但是使用 thread 协程,我没有看到在完成时得到通知的合理方法.另外,我也不能将 progressBar.visibility = View.GONE 放在新线程中,因为只有 UI 线程允许进行此类操作.

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.

我是协程的新手,所以我不太明白我在这里缺少什么.

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

推荐答案

要使用协程,您需要满足以下条件:

To use a coroutine you need a couple of things:

  • 实现CoroutineScope接口.
  • JobCoroutineContext 实例的引用.
  • 在调用在后台线程中运行代码的函数时,使用暂停函数修饰符来暂停协程而不阻塞主线程.
  • 使用 withContext(Dispatchers.IO) 函数在后台线程中运行代码并使用 launch 函数来启动协程.
  • 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":

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 execute() = launch {
        onPreExecute()
        val result = doInBackground() // runs in background thread without blocking the Main Thread
        onPostExecute(result)
    }

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

    // Runs on the Main(UI) Thread
    private fun onPreExecute() {
        // show progress
    }

    // Runs on the Main(UI) Thread
    private fun onPostExecute(result: String) {
        // hide progress
    }
}

使用 ViewModel 代码更简洁使用 viewModelScope:

class MyViewModel : ViewModel() {
    
    fun execute() = viewModelScope.launch {
        onPreExecute()
        val result = doInBackground() // runs in background thread without blocking the Main Thread
        onPostExecute(result)
    }

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

    // Runs on the Main(UI) Thread
    private fun onPreExecute() {
        // show progress
    }

    // Runs on the Main(UI) Thread
    private fun onPostExecute(result: String) {
        // hide progress
    }
}

要使用 viewModelScope,在应用的 build.gradle 文件的依赖项中添加下一行:

To use viewModelScope add next line to dependencies of the app's build.gradle file:

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$LIFECYCLE_VERSION"

在撰写本文时 final LIFECYCLE_VERSION = "2.3.0-alpha04"

这篇关于AsyncTask 作为 kotlin 协程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆