filmov
tv
Why You Need To Use Coroutines in Android and How | MVVM Example

Показать описание
In this video Learn how to use Coroutine with Retrofit and MVVM
What is Coroutine
In Android executing a long-running operation like network calls, database operations or large computations blocks the **Main thread** which happens to be the **UI thread** responsible for rendering the UI widgets that the user can see. To avoid blocking the main thread we need to run these long-running operations on a separate thread outside of the main thread which is known as background thread. This is where Coroutine and multi-threading come in. Coroutine basically simplifies asynchronous programming on android and allows operations to run without blocking the main thread.
Lets look at some examples from the previous parts of the series:
This is a method to fetch **rick and morti characters** from an ApiService, this process is going to block the UI thread because it is been performed over a network call and we don't want this to happen that is why we used the built in Callback in the Retrofit library to execute it on the background thread.
But we can make this function shorter and more readable with the use of Coroutine
Modify the project to use Coroutine
Now let's modify this project to use Coroutine. This is the third part of two previous lessons, if you are new to Retrofit and MVVM then you need to check out the part one and two.
To use coroutines and the built-in scopes we add the following dependencies
Now let us modifier the method definition in the ApiService to use coroutine
To make fetchCharacters use coroutine, first, we added the suspend modifier at the beginning of the function then we remove the Call interface from the return type.
Back to the repository, we will have an error
This is because the suspend function can either be called in another suspend function or a coroutine scope so we need to also add a suspend function to this method.
Coroutine Scope
To finally execute this function we need a coroutine scope. Coroutines follow a principle of structured concurrency which means that new coroutines can only be launched in a specific CoroutineScope. If you create a scope and don't clear it at the end of the operation it can cause a memory leak and you wouldn't want that. In android, there is a built-in scope that can be used for specific classes.
- viewModelScope - This is scoped to the ViewModel class so that a coroutine automatically gets canceled when the ViewModel class is cleared.
- lifecycle scope- This is scoped to Activities and Fragments so a coroutine launched by it gets canceled when they are destroyed.
You can also create your own scope, create a job and attach it to the scope so that you can call cancel on the job once the execution is completed.
tutorialsEU offers you free video tutorials about programming and development for complete beginners up to experienced programmers.
This includes C#, Unity, Python, Android, Kotlin, Machine Learning, etc.
What is Coroutine
In Android executing a long-running operation like network calls, database operations or large computations blocks the **Main thread** which happens to be the **UI thread** responsible for rendering the UI widgets that the user can see. To avoid blocking the main thread we need to run these long-running operations on a separate thread outside of the main thread which is known as background thread. This is where Coroutine and multi-threading come in. Coroutine basically simplifies asynchronous programming on android and allows operations to run without blocking the main thread.
Lets look at some examples from the previous parts of the series:
This is a method to fetch **rick and morti characters** from an ApiService, this process is going to block the UI thread because it is been performed over a network call and we don't want this to happen that is why we used the built in Callback in the Retrofit library to execute it on the background thread.
But we can make this function shorter and more readable with the use of Coroutine
Modify the project to use Coroutine
Now let's modify this project to use Coroutine. This is the third part of two previous lessons, if you are new to Retrofit and MVVM then you need to check out the part one and two.
To use coroutines and the built-in scopes we add the following dependencies
Now let us modifier the method definition in the ApiService to use coroutine
To make fetchCharacters use coroutine, first, we added the suspend modifier at the beginning of the function then we remove the Call interface from the return type.
Back to the repository, we will have an error
This is because the suspend function can either be called in another suspend function or a coroutine scope so we need to also add a suspend function to this method.
Coroutine Scope
To finally execute this function we need a coroutine scope. Coroutines follow a principle of structured concurrency which means that new coroutines can only be launched in a specific CoroutineScope. If you create a scope and don't clear it at the end of the operation it can cause a memory leak and you wouldn't want that. In android, there is a built-in scope that can be used for specific classes.
- viewModelScope - This is scoped to the ViewModel class so that a coroutine automatically gets canceled when the ViewModel class is cleared.
- lifecycle scope- This is scoped to Activities and Fragments so a coroutine launched by it gets canceled when they are destroyed.
You can also create your own scope, create a job and attach it to the scope so that you can call cancel on the job once the execution is completed.
tutorialsEU offers you free video tutorials about programming and development for complete beginners up to experienced programmers.
This includes C#, Unity, Python, Android, Kotlin, Machine Learning, etc.
Комментарии