Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get lifecycle.coroutineScope with new androidx.lifecycle:*:2.2.0-alpha01

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 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?

like image 822
Rajarshi Avatar asked May 08 '19 07:05

Rajarshi


People also ask

How do you make a CoroutineScope?

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).

How do I get ViewModelScope on my Android?

For ViewModelScope , use androidx. lifecycle:lifecycle-viewmodel-ktx:2.4. 0 or higher. For LifecycleScope , use androidx.

What is CoroutineScope?

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.

How do I use LiveData with coroutines?

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.

Is it possible to get lifecyclescope in androidx?

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.

What happens to any coroutine launched when the lifecycle is destroyed?

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:

What versions of Android have the repeatonlifecycle API?

Note: The repeatOnLifecycle API is available only in versions of the androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-alpha01 library and higher.

What are lifecycle-aware components in Android?

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.


2 Answers

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" 
like image 135
Matt Butlar Avatar answered Oct 11 '22 07:10

Matt Butlar


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.

like image 22
Jimale Abdi Avatar answered Oct 11 '22 06:10

Jimale Abdi