Coroutines in Kotlin - BigStep Technologies
10479
post-template-default,single,single-post,postid-10479,single-format-standard,ajax_fade,page_not_loaded,,qode_grid_1200,vss_responsive_adv,qode-child-theme-ver-1.0.0,qode-theme-ver-1.0.0,qode-theme-bridge child,bridge-child,wpb-js-composer js-comp-ver-5.1.1,vc_responsive

Coroutines in Kotlin

0
Coroutines in Kotlin

Asynchronous or non-blocking programming is an integral part of the development environment. When building server-side, desktop, or mobile applications, it’s essential to provide an experience that’s not only fluid from the user’s perspective but also scalable when needed.

Kotlin solves this problem flexibly by providing coroutine support at the language level and delegating most functions to libraries.

In addition to opening the door to asynchronous programming, coroutines also provide a number of other possibilities, such as concurrency and actors. Let’s read more about coroutines in Kotlin.

Coroutine:

      • Coroutine = Co + Routine.
      • Here Co means cooperation and Routine means function.
      • It means when functions cooperate with each other we call it coroutine.
      • It can execute a few lines of functionA and then execute a few lines of functionB and then again a few lines of functionA and so on. This will be helpful when a thread is sitting idle and not doing anything, in that case, it can execute a few lines of another function. This way, it can take full advantage of the thread. Ultimately cooperation helps in multitasking.
      • Overall Coroutines make multitasking very easy.
      • We can say Coroutines and Thread both are used for multitasking. Threads are managed by the OS and coroutines are managed by the user. Coroutines can execute a few lines of a function by using the advantages of cooperation.
      • We can say Coroutines are light weight threads. Light weight thread means it doesn’t map on native thread, so it doesn’t require context switching on the processor. So these are faster.
      • Types of Coroutines ->
        1. Stackless
        2. Stackfull
      • Kotlin supports Stackless coroutines. That means coroutines don’t have their own stack.  So these don’t map on the native thread.

Dispatchers:

      • Dispatchers help coroutines to decide on which thread work has to be done.
      • There are majorly three types of Dispatchers which are IO, Default, and Main
      • IO dispatcher is used to do the network and disk-related work. Default is used to do the CPU-intensive work. The Main is the UI thread of Android.

Suspend function:

      • Suspend function is a function that could be started, paused, and resumed.
      • Suspend functions can only be called from a coroutine or another suspend function.

Launch v/s Async ->

      • There are two functions to start the coroutine.
      • launch {}  and async {}
      • The difference between launch and async is launch doesn’t return anything but async returns an instance of Deferred<T>, which has a function await() that returns the result of coroutine. 
      • When we call await() function, it waits for the result, and doesn’t allow the execution to go ahead.

withContext vs Async:

      • withContext is another way of writing async where we don’t need to call await().
      • When we use withContext it executes in series not in parallel.
      • We should use withContext when we don’t need the parallel execution.
      • We should use async when we need parallel execution.
      • Both withContext and async are used to get results which are not possible with the launch.
      • Use withContext to return the result of a single task.
      • Use async to return the result of multiple tasks.

Scope:

      • lifecycleScope
      • viewModelScope
      • GlobalScope

coroutineScope v/s supervisorScope:

      • coroutineScope will cancel whenever any of its children fails.
      • If we want to continue when any of its children fail, we use supervisorScope.
      • supervisorScope wouldn’t cancel other children if any of them fails.

Exception handling in Coroutines:

    • There are two types of exception handling in coroutine.
      1. Using try- catch block.
  1. Using handler

Conclusion

The main difference is that the coroutineScope is canceled whenever one of its children fails. If we want to continue other tasks even if one fails, we use supervisorScope. SupervisorScope does not cancel the other children when one of them fails. This is how exception handling can be done in Kotlin Coroutines.

Vishnu Soni

Google Certified Senior Android Developer at BigStep Technologies with a passion to learn new things.

Get Connected with our Experts

Request a Quote

Tell us about your requirements and we'll get back to you soon.