The life cycle of the kotlin collaboration is bound to the jetpack component

Time:2022-5-11

How does Android use coprocessing

On Android, Xiecheng can help solve two major problems:

Manage long – running tasks that may block the main thread and cause UI jams

Safely invoke network or disk operations on the main thread

The best way to use collaborative processes on Android is to use official architecture components, which provide support for collaborative processes At present, ViewModel, lifecycle, livedata and room components provide first-class support for collaborative programs

1、ViewModel ViewModelScope

The main support for ViewModel is to provide a “coroutinescope” called “viewmodelscope” on ViewModel. All collaborations started on viewmodelscope will be automatically cancelled when viewmodelscope is destroyed This can effectively prevent resource leakage caused by forgetting to cancel the task

In fact, the implementation of viewmodelscope is very simple, that is, it is a dispatcher with viewmodelscope Main , supervisorjob, when , ViewModel When clear(), call job Cancel(), because of structural concurrency, all collaborations started within the scope of viewmodelscope will be cascaded and cancelled

2、Lifecycle LifecycleScope

Each object (lifecycle) with a lifecycle has a lifecycle scope, and all collaborations started within its scope will be cancelled when the lifecycle object is destroyed The {coroutinescope} of a lifecycle object can be through} lifecycle Coroutinescope or lifecycle owner Get the lifecyclescope attribute

3. Suspend lifecycle related orchestrations

In # activity # or # fragment # we sometimes need to wait until a certain life cycle method, or at least after a certain life cycle method before executing a certain task. For example, the page state must be at least # started # before executing # fragment transaction. The life cycle component also supports this requirement Lifecycle provides lifecycle whenCreated, lifecycle. whenStarted, lifecycle. Whenresumed: three methods. If the state of the page is not at least in the required minimum state, the process will be suspended

If the collaboration is in the active state after it is started through the whenxxx method above and the page is destroyed, the collaboration will be automatically cancelled and will go to the “finally” block below. In the “finally”, you need to check the state of the page and decide what action to do

Note here: if the page “restart” is restarted, but the collaboration will not be restarted, in short, ensure that the information is correct

4. Using coroutines in livedata

5. Room’s support for collaborative process

Room from V2 1 start supporting collaboration

Jetpack knowledge points actual combat series 3: using coroutines, retrofit and Moshi to realize network data request

Recommended Today

Swift — difference between struct and class

Classes and structures in swift are very similar. They both have object-oriented features such as defining and using attributes, methods, subscripts and constructors. However, structures do not have inheritance, nor do they have the ability to force type conversion at runtime, use destructors and reference counting! In swift, struct is the value type and class […]