Рет қаралды 34,666
🏆 My Online Courses
⭐Discount Coupon: LAUNCH-STEVDZA-SAN
stevdza-san.com
🐱👤 Wanna become a member? Join!
/ @stevdzasan
📸 Instagram
/ stevdza_san
Kotlin coroutines provide an API that enables you to write asynchronous code. With Kotlin coroutines, you can define a CoroutineScope, which helps you to manage when your coroutines should run. Each asynchronous operation runs within a particular scope.
Global scope is used to launch top-level coroutines which are operating on the whole application lifetime and are not cancelled prematurely.
A LifecycleScope is defined for each Lifecycle object. Any coroutine launched in this scope is canceled when the Lifecycle is destroyed.
A ViewModelScope is defined for each ViewModel in your app. Any coroutine launched in this scope is automatically canceled if the ViewModel is cleared. Coroutines are useful here for when you have work that needs to be done only if the ViewModel is active. For example, if you are computing some data for a layout, you should scope the work to the ViewModel so that if the ViewModel is cleared, the work is canceled automatically to avoid consuming resources.
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
Other videos:
The Basics - • The Basics - Kotlin Co...
Jobs & Cancellation - • Jobs and Cancellation ...
runBlocking - • runBlocking - Kotlin C...
withContext - • withContext() - Kotlin...
🕒Timestamps:
0:00 - Introduction
3:00 - Create Coroutine Scope
7:50 - GlobalScope
10:47 - lifecycleScope & viewModelScope