我应该在我的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
在build.gradle
将图书馆更改为
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
。
删除,如果添加:
kotlin {
experimental {
coroutines "enable"
}
}
在代码中将launch
更改为GlobalScope.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 {
添加局部变量job
并初始化它:
private lateinit var job: Job
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
job = Job()
}
创建一个协程上下文并在Activity destroy上取消它:
override fun onDestroy() {
job.cancel()
super.onDestroy()
}
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
对于片段(与Activity
相同)
实施CoroutineScope:
class YourFragment : Fragment(), CoroutineScope {
创建一个局部变量job
并在onCreate()
中初始化它。 (我试图写private val job: Job = Job()
,但碰到问题,在ViewPager
你将创建Fragment
s和他们的工作。因为我们将在job
刷卡期间取消onDestroy()
中的ViewPager
,我们应该重新创造这项工作)。
private lateinit var job: Job
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
job = Job()
}
创建一个协程上下文并在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
内部。所以我用交互者重新编写了代码。
我的队友帮我找到了解决方案。我不得不将协同程序版本增加到1.0.0-RC1。对于每个可能不了解使用Android协同程序的变化的人:
coroutineScope
)。我希望我会帮助别人。不要浪费时间。编程愉快!