使用Kotlin 1.3迁移到Android中的Kotlin协同程序

Pat*_*iak 11 android kotlin kotlinx.coroutines

我应该在我的build.gradle文件中更改或导入类以在我的Android项目中使用Kotlin 1.3使用稳定的协程函数?

关于我的协同程序的碎片 build.gradle

implementation "org.jetbrains.kotlin:kotlin-coroutines-core:$coroutines_version" implementation "org.jetbrains.kotlin:kotlin-coroutines-android:$coroutines_version"

当然我使用的是Android Studio 3.3 Preview

Coo*_*ind 24

build.gradle将库更改为

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'.

删除,如果添加:

kotlin {
    experimental {
        coroutines "enable"
    }
}
Run Code Online (Sandbox Code Playgroud)

在代码更改launchGlobalScope.launch(Dispatchers.IO)GlobalScope.launch(Dispatchers.Main).

UPDATE

请使用本地协程上下文而不是全局范围(例如,请参阅http://kotlinlang.org/docs/reference/coroutines/coroutine-context-and-dispatchers.html).

对于活动

请参阅https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/coroutines-guide-ui.md.

实施CoroutineScope:

class YourActivity : AppCompatActivity(), CoroutineScope {
Run Code Online (Sandbox Code Playgroud)

添加局部变量job并初始化它:

private lateinit var job: Job

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    job = Job()
}
Run Code Online (Sandbox Code Playgroud)

创建一个协程上下文并在Activity destroy上取消它:

override fun onDestroy() {
    job.cancel()
    super.onDestroy()
}

override val coroutineContext: CoroutineContext
    get() = Dispatchers.Main + job
Run Code Online (Sandbox Code Playgroud)

对于片段(与in相同Activity)

实施CoroutineScope:

class YourFragment : Fragment(), CoroutineScope {
Run Code Online (Sandbox Code Playgroud)

创建一个局部变量job并将其初始化onCreate().(我试着写private val job: Job = Job(),但碰到了,在问题ViewPager您将创建FragmentS和他们的工作.我们将取消jobonDestroy()刷卡过程中ViewPager,我们应该重新创建工作).

private lateinit var job: Job

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    ...
    job = Job()
}
Run Code Online (Sandbox Code Playgroud)

创建一个协程上下文并在Fragment destroy上取消它:

override val coroutineContext: CoroutineContext
    get() = Dispatchers.Main + job // You can use different variants here. 

override fun onDestroy() {
    job.cancel()
    super.onDestroy()
}
Run Code Online (Sandbox Code Playgroud)

一个发布的例子

launch像往常一样使用:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    launch {
        // Wait for result of I/O operation without blocking the main thread.
        withContext(Dispatchers.IO) {
            interactor.getCountry().let {
                countryName = it.name
            }
        }

        // Update views in the UI thread.
        country.updateCaption(countryName)
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的情况下,当我使用通常回调的API请求时出现问题.一个launch回调里面内部没有被调用.所以我用交互者重新编写了代码.


Pat*_*iak 6

我的队友帮我找到了解决方案.我不得不将协同程序版本增加到1.0.0-RC1.对于可能不了解使用Android协同程序的变化的每个人:

  • 我不得不将协程的UI上下文更改为Dispatchers.Main
  • 我使用旧的实验协同程序版本(可能是0.23),因此对于每个不知道的人 - 现在启动已弃用,您应该使用结构化并发(例如coroutineScope).
  • 现在异步函数不能在范围之外运行.

我希望我会帮助别人.不要浪费时间.编程愉快!