Read More
Read now

Kotlin Coroutines and suspend function with retrofit

Save

This blog covers Kotlin Coroutines and suspend function with retrofit.

Coroutines ->

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

  1. Long running task which can block main thread
  2. Main-safety allows you to ensure that any suspend function can be called from the main thread

Coroutines build upon regular functions by adding two new operations. In addition to invoke (or call) and return, coroutines add suspend and resume.

Suspend Function ->

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.

Use CoroutineScope with Android Architecture components ->

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:

Kotlin Coroutines and suspend function with retrofit ->

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

Required Dependencies ->

Activity ->

ViewModel Class ->

 

Retrofit Api Interface ->

Retrofit class ->

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.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

css.php

Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!

Deniel Kerr

Owner and Founder. Opencart

Talk to Sales
Start a Project

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home