Android Coroutine Extensions

Android Coroutine Extensions

ยท

2 min read

Why Coroutine?

If get to know something new by reading my articles, don't forget to endorse me on LinkedIn

A coroutine can provide a very high level of concurrency with very small overhead. Multiple threads can also provide parallelism but there is blocking and context switching. Coroutine suspends the thread and does not block it so that it can switch to another work.

Kotlin Extension Function?

Basically, Kotlin Extension Function provides a facility to add methods to class without inheriting a class or using any type of design pattern. The created extension functions are used as a regular function inside that class.

Why This "Android Coroutine Extensions"?

Kotlin's Coroutine Extension functions to fire CoroutineScope from AppCompactActivity/Fragment/ViewModel which is tied to it's parent CoroutineScope, so that If the parent is no loger exists, cancel any ongoing operation under the parent CoroutineScope.

Basic Usages:

  • Lets assume that when an Activity is started & you need to do some operation on the activity start. But user leave the screen and the ongoing Operation should be cancelled automatically.

  • For API request, assume you are calling an API from your ViewModel with a different Coroutine Scope and leave the screen but the operation is still ongoing, because it was not instructed to cancel any ongoing operation when parent is no longer exist.

By using those extension functions you don't have to worry about the cancellation of ongoing operations according to the parent's LifeCycle.

Let's a look at the available functions.

Follow Me on LinkedIn:

ย