On 7th May, 2019 androidx.lifecycle:*:2.2.0-alpha01
was released announcing:
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
:
either via
lifecycle.coroutineScope
orlifecycleOwner.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?
The easiest way to create a coroutine scope object is by using the CoroutineScope factory function 1. It creates a scope with provided context (and an additional Job for structured concurrency if no job is already part of the context).
For ViewModelScope , use androidx. lifecycle:lifecycle-viewmodel-ktx:2.4. 0 or higher. For LifecycleScope , use androidx.
CoroutineScope is the interface that define the concept of Coroutine Scope: to launch and create coroutines you need a one. GlobalScope is a instance of scope that is global for example. CoroutineScope() is a global function that creates a CoroutineScope.
ViewModel + LiveData You could use a MutableLiveData like so: But, since you will be exposing this result to your view, you can save some typing by using the liveData coroutine builder which launches a coroutine and lets you expose results through an immutable LiveData. You use emit() to send updates to it.
On 7th May, 2019 androidx.lifecycle:*:2.2.0-alpha01 was released announcing: 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: But it seems that I'm not able to find none of them.
Any coroutine launched in this scope is canceled when the Lifecycle is destroyed. You can access the CoroutineScope of the Lifecycle either via lifecycle.coroutineScope or lifecycleOwner.lifecycleScope properties. The example below demonstrates how to use lifecycleOwner.lifecycleScope to create precomputed text asynchronously:
Note: The repeatOnLifecycle API is available only in versions of the androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-alpha01 library and higher.
Lifecycle-aware components perform actions in response to a change in the lifecycle status of another component, such as activities and fragments. These components help you produce better-organized, and often lighter-weight code, that is easier to maintain. This table lists all the artifacts in the androidx.lifecycle group.
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
For ViewModelScope, use androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-beta01
or higher.
For LifecycleScope, use androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha01
or higher.
at this time 2.3.0-rc01
is available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With