Coroutine Flashcards

1
Q

What is Coroutine ?

A

“A framework to manage concurrency in a more performant and simple way with its lightweight thread which is written on top of the actual threading framework to get the most out of it by taking the advantage of cooperative nature of functions.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why there is a need for Kotlin Coroutines?

A

Let’s take very standard use-case of an Android Application which is as follows:
Fetch User from the server.
Show the User in the UI.

When we call the fetchAndShowUser function, it will throw the NetworkOnMainThreadException as the network call is not allowed on the main thread.
There are many ways to solve that. A few of them are as follows:

  1. Using Callback:Here, we run the fetchUser in the background thread and we pass the result with the callback.
  2. Using RxJava:Reactive world approach. This way we can get rid of the nested callback.
  3. Using Coroutines:Yes, coroutines.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What Dispatchers in Coroutines ?

A

Dispatchers: Dispatchers helps coroutines in deciding the thread on which the work has to be done. There are majorly three types of Dispatchers which are as 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. Main is the UI thread of Android.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is suspend function in Coroutines ?

A

Suspend function is a function that could be started, paused and resume.

Suspend functions are only allowed to be called from a coroutine or another suspend function. You can see that theasyncfunction which includes the keywordsuspend. So, in order to use that, we need to make our functionsuspendtoo.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to start coroutines in Kotlin ?

A

There are two functions in Kotlin to start the coroutines which are as follows:

  1. launch{}
  2. async{}

Launch vs Async in Kotlin Coroutines :

The difference is that the launch{} does not return anything and the async{}returns an instance of Deferred, which has an await()function that returns the result of the coroutine like we have future in Java in which we do future.get() to the get the result.

In other words:

launch: fire and forget
async: perform a task and return a result

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is withContext() in Kotlin ?

A

withContext is nothing but another way of writing the async where we do not have to write await().

When we use withContext, it will run in series instead of parallel.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is thumb rules of coroutines ?

A

The thumb-rules:

  1. Use withContext when you do not need the parallel execution.
  2. Use async only when you need the parallel execution.
  3. Both withContext and async can be used to get the result which is not possible with the launch.
  4. Use withContext to return the result of a single task.
  5. Use async for results from multiple tasks that run in parallel.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Coroutine scopes in Kotlin ?

A

Scopes in Kotlin Coroutines :

Scopes in Kotlin Coroutines are very useful because we need to cancel the background task as soon as the activity is destroyed. Here, we will learn how to use scopes to handle these types of situations.

As soon as the activity is destroyed, the task will get canceled if it is running because we have defined the scope.

When we need the global scope which is our application scope, not the activity scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly