It is known that async and launch are the two ways to start the coroutine. Since It is known that async is used to get the result back, & should be used only when we need the parallel execution, whereas the launch is used when we do not want to get the result back and is used for the operation such as updating of data, etc. As we know that async is the only way till now to start the coroutine and get the result back, but the problem with async arises when we do not want to make parallel network calls. It is known when async is used, one needs to use the await() function, which leads to blocking of the main thread, but here comes the concept of withContext which removes the problem of blocking the main thread.
withContext is nothing but another way of writing the async where one does not have to write await(). When withContext, is used, it runs the tasks in series instead of parallel. So one should remember that when we have a single task in the background and want to get back the result of that task, we should use withContext. Let us take an example which demonstrates the working of the withContext:
Let’s run the two tasks in parallel using async-await and then using withcontext and see the difference between the two.
Here using async, both the task run in parallel. Now let’s use the withContext and do the same task in series with withContext.
Here one can see that in withContext everything is the same, the only difference is that here we do not have to use the await() function, and tasks are executed in a serial manner. Since here multiple tasks have been taken, one should remember that async should be used with multiple tasks and withContext should be used with a single task. Now let’s take an example and try to understand withContext in detail and how it is executed.
function 2 executes faster than function 1 since function 2 has less delay then function 1. Since withContext is a suspend call, that is it won’t go to the next line until it finished. Since withContext is a suspend call and does not block the main thread, we can do other tasks while the IO thread is busy in executing function1 and function 2.
- Kotlin Coroutines on Android
- Dispatchers in Kotlin Coroutines
- Launch vs Async in Kotlin Coroutines
- Suspend Function In Kotlin Coroutines
- runBlocking in Kotlin Coroutines with Example
- Jobs, Waiting, Cancellation in Kotlin Coroutines
- Scopes in Kotlin Coroutines
- Kotlin Data Types
- Hello World program in Kotlin
- Kotlin | Retrieve Collection Parts
- Destructuring Declarations in Kotlin
- Why Kotlin will replace Java for Android App Development
- DatePicker in Kotlin
- Kotlin labeled continue
- Introduction to Kotlin
- Kotlin Type Conversion
- Kotlin Exception Handling | try, catch, throw and finally
- Kotlin if-else expression
- Kotlin Environment setup for Command Line
- Kotlin constructor
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.