Android App Development
iOS App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 188.8.131.52 release ready!
Owner and Founder. Opencart
This blog covers Kotlin Coroutines and suspend function with retrofit.
Coroutines can be used on Android to simplify async code. Android Jetpack APIs now have first class support for Kotlin Coroutines. Now, Kotlin 1.3 supports coroutines.
Coroutines are helpful in two main problems ->
Coroutines build upon regular functions by adding two new operations. In addition to invoke (or call) and return, coroutines add suspend and resume.
A suspending function is just a regular Kotlin function with an additional suspend modifier which indicates that the function can suspend the execution of a coroutine.
You can only call suspend functions from other suspend functions, or by using a coroutine builder like launch to start a new coroutine.
We use call back functions when we get response from our Async task. Suspend and resume work together to replace callbacks.
To understand suspend functions, we should also know about provided dispatchers by Kotlin.
To specify where the coroutines should run, Kotlin provides three dispatchers that you can use:
Lets, see this with an example -> We are calling our api through coroutines. So, we use Dispatchers.IO.
When we call callApi() suspend method, then it suspends our coroutine. The coroutine on the main thread will be resumed with the result as soon as the withContext block is complete.
Note: Using suspend doesn’t tell Kotlin to run a function on a background thread. It’s normal for suspend functions to operate on the main thread.
You can associate CoroutineScope implementations with a component lifecycle. This lets you avoid leaking memory or doing extra work for activities or fragments that are no longer relevant to the user. Using Jetpack components, they fit naturally in a ViewModel. Because a ViewModel isn’t destroyed during configuration changes (such as screen rotation). So, you don’t have to worry about your coroutines getting cancelled or restarted.
You can start coroutines in one of two ways -> launch & async
Some Architecture components, including ViewModel and LifeCycle , include built-in support for coroutines through their own CoroutinesScope members.
For example, ViewModel includes a built-in viewModelScope. This provides a standard way to launch coroutines within the scope of the ViewModel, as shown in the following example:
We will cover this blog with launch example.
Now, lets see the example of Kotlin Coroutines and suspend function with retrofit.
In the below example, AndroidX is migrated.
To learn about androidX, you can refer this link -> AndroidX
If you want to migrate your project with AndroidX, then simply follow these steps ->
Android Studio(Android Studio 3.2 and higher) -> Refactor -> Migrate To AndroidX
To learn more about kotlin & Coroutines, you can also refer this link -> Coroutines Guide
That’s done. Hope, this blog will be helpful to you.
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Excellent work, fast, good quality and understood the brief perfectly! Quick responses developing the project and very good cooperation. I suggest to anyone.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.