使用 Kotlin 1.3 在 Android 中迁移到 Kotlin 协程 [英] Migrate to Kotlin coroutines in Android with Kotlin 1.3
问题描述
我应该在我的 build.gradle
文件中更改什么或在类中导入以在我的带有 Kotlin 1.3 的 Android 项目中使用稳定的协程函数?
What should I change in my build.gradle
file or import in classes to use stable coroutine functions in my Android project with Kotlin 1.3 ?
我的build.gradle
实现org.jetbrains.kotlin:kotlin-coroutines-core:$coroutines_version"实现org.jetbrains.kotlin:kotlin-coroutines-android:$coroutines_version"
当然我用的是 Android Studio 3.3 预览版
Of course I use Android Studio 3.3 Preview
推荐答案
在 build.gradle
中将一个库更改为
In build.gradle
change a library to
实现'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
.
删除,如果已添加:
kotlin {
experimental {
coroutines "enable"
}
}
在代码中将 launch
更改为 GlobalScope.launch(Dispatchers.IO)
或 GlobalScope.launch(Dispatchers.Main)
.
In code change launch
to GlobalScope.launch(Dispatchers.IO)
or GlobalScope.launch(Dispatchers.Main)
.
更新
请使用本地协程上下文而不是全局范围(例如,请参见 http://kotlinlang.org/docs/reference/coroutines/coroutine-context-and-dispatchers.html).
Please, use local coroutine context instead of global scope (see, for instance, 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 {
添加局部变量job
并初始化:
Add a local variable job
and initialize it:
private lateinit var job: Job
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
job = Job()
}
创建一个协程上下文并在 Activity destroy 时取消它:
Create a coroutine context and cancel it on Activity destroy:
override fun onDestroy() {
job.cancel()
super.onDestroy()
}
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
对于 Fragment(与 Activity
中的相同)
实现 CoroutineScope:
Implement CoroutineScope:
class YourFragment : Fragment(), CoroutineScope {
创建一个局部变量job
并在onCreate()
中初始化它.(我试图编写 private val job: Job = Job()
,但遇到了在 ViewPager
中你将创建 Fragment
和它们的作业.因为我们会在 ViewPager
中滑动时取消 onDestroy()
中的 job
,所以我们应该重新创建作业.
Create a local variable job
and initialize it in onCreate()
. (I tried to write private val job: Job = Job()
, but bumped into problem that in ViewPager
you will create Fragment
s and their jobs. As we will cancel the job
in onDestroy()
during swiping in ViewPager
, we should recreate the job).
private lateinit var job: Job
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
job = Job()
}
创建一个协程上下文并在 Fragment destroy 时取消它:
Create a coroutine context and cancel it on Fragment destroy:
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job // You can use different variants here.
override fun onDestroy() {
job.cancel()
super.onDestroy()
}
启动示例
照常使用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)
}
}
就我而言,当我使用带有通常回调的 API 请求时出现问题.回调中的 launch
内部没有被调用.所以我用交互器重写了那个代码.
In my case a problem occured when I used API requests with usual callbacks. A launch
interior inside a callback hasn't been called. So I rewrote that code with interactors.
这篇关于使用 Kotlin 1.3 在 Android 中迁移到 Kotlin 协程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!