gpt4 book ai didi

How to get lifecycle.coroutineScope with new androidx.lifecycle:*:2.2.0-alpha01(如何使用新的androidx.lifecycle.coroutineScope。生命周期:*:2.2.0-alpha01)

转载 作者:bug小助手 更新时间:2023-10-25 15:27:47 24 4
gpt4 key购买 nike



On 7th May, 2019 androidx.lifecycle:*:2.2.0-alpha01 was released announcing:

2019年5月7日,androidx.Life Cycle:*:2.2.0-alpha01发布,宣布:




This release adds new features that adds support for Kotlin coroutines for Lifecycle and LiveData. Detailed documentation on them can be found here.




On documentation it's mentioned that I can get the LifecycleScope:

在文档中提到,我可以获得LifecycleScope:




either via lifecycle.coroutineScope or lifecycleOwner.lifecycleScope properties




But it seems that I'm not able to find none of them. My current dependencises are:

但是我好像一个也找不到。我目前的依赖关系是:



def lifecycle_ver = "2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_ver"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_ver"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_ver"

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1'


What may be the cause and how do get these apis?

可能的原因是什么?如何获得这些API?


更多回答

you can find the relevant versions here , mvnrepository.com/artifact/androidx.lifecycle/… , mvnrepository.com/artifact/androidx.lifecycle/… , mvnrepository.com/artifact/androidx.lifecycle/…

您可以在此处找到相关版本,mvnrepository.com/artifact/androidx.lifecycle/…,mvnrepository.com/artifact/androidx.lifecycle/…,mvnrepository.com/artifact/androidx.lifecycle/…

优秀答案推荐

I actually spent a couple hours trying to figure this out myself and it turns out it is in a new package that only exists as of the alpha. Add this and you should be good to go.

事实上,我自己花了几个小时试图弄清楚这一点,结果发现它是在一个新的包中,它只作为阿尔法存在。加上这个,你应该就可以走了。



implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_ver"


The accepted answers is working, but I'm misused for the first time, so I'm trying to make it clear, the current version of lifecycle is "2.1.0" and lifecycleScope, and ViewModelScope is not available in this version, to get them use

公认的答案是有效的,但我第一次被误用了,所以我试图澄清一下,当前版本的LIFECYCLE是“2.1.0”和lifecycleScope,而ViewModelScope在这个版本中不可用,以便让他们使用


For ViewModelScope,

对于ViewModelScope,


use androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-beta01 or higher.

使用androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-beta01或更高版本。


For LifecycleScope,

对于LifescleScope,


use androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha01 or higher.

使用androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha01或更高版本。


at this time 2.4.1 is available.

目前,2.4.1是可用的。



I've just added this two lines of code to my build.gradle (App) and that worked like a charm. I can import lifecycleScope without any problems.

我刚刚将这两行代码添加到我的build.gradle应用程序中,效果非常好。我可以导入Life CleScope而不会有任何问题。


implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.2'


Try this line of code in Build.Gradle (App Level)

在build.gradle(应用程序级)试用这行代码


implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.2.0")

更多回答

Are you sure you did that exact line? I pulled up the source for it and have my own code running with it using lifecycleScope. What is it doing when you add it and try to reference lifecycleScope from a lifecycleOwner?

你确定你的台词跟你说的一模一样?我找到了它的源代码,并使用lifecycleScope运行了我自己的代码。当您添加它并尝试从lifecycleOwner引用lifecycleScope时,它在做什么?

Even in an activity you should be able to do lifecycle.lifecycleScope since the activity is a lifecycle owner. What error is it giving you when you add the runtime-ktx dependency?

即使在活动中,您也应该能够执行lifecycle.lifecycleScope,因为该活动是生命周期所有者。当您添加运行时-ktx依赖项时,它会给您带来什么错误?

Thanks, works for me. Just use lifecycleScope.launch {...} from acrivity

谢谢,对我很管用。只需使用生命周期作用域.Launch{...}即可

OMG. I'm searching for this for hours! This works. (The jetpack / ktx documentation is absolutely miserable. So many different sites but none shows a clear list of available packages and versions. Shame on you jetpack team!)

天哪。我已经找了好几个小时了!这很管用。(Jetpack/KTX文档非常糟糕。有这么多不同的站点,但没有一个站点显示可用包和版本的清晰列表。你们喷气背包队真丢人!)

Source: developer.android.com/topic/libraries/architecture/…

消息来源:developer.android.com/topic/libraries/architecture/…

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com