I am new to coroutines, I understand launch and async but still confusing part is Deferred. What is Deferred? and difference between Job and Deferred. Clear explanation and example is more helpful. Thanks in advance.
async starts a new coroutine and returns a Deferred object. Deferred represents a concept known by other names such as Future or Promise : it stores a computation, but it defers the moment you get the final result; it promises the result sometime in the future.
Definition of Job A Job is a cancellable thing with a life-cycle that culminates in its completion. Coroutine job is created with launch coroutine builder. It runs a specified block of code and completes on completion of this block.
Get started with coroutines Well, there is a bit of difference between both, launch does not carry resulting value, it just returns job which can be canceled later, however async returns a Deferred which is a lightweight non-blocking future that represents a promise to provide result later.
Coroutines are very similar to threads. However, coroutines are cooperatively multitasked, whereas threads are typically preemptively multitasked. Coroutines provide concurrency but not parallelism.
So job is sort of an object that represents a coroutine's execution and is related to structured concurrency, e.g. you can cancel a job, and all the children of this job will be also cancelled.
From docs:
Job is a cancellable thing with a life-cycle that culminates in its completion.
Deferred is some kind of analog of Future in Java: in encapsulates an operation that will be finished at some point in future after it's initialization. But is also related to coroutines in Kotlin.
From documentation:
Deferred value is a non-blocking cancellable future — it is a Job that has a result.
So, Deferred is a Job that has a result:
A
deferredvalue is aJob. Ajobin thecoroutineContextofasyncbuilder represents the coroutine itself.
An example:
someScope.launch {     val userJob: Deferred<User> = async(IO) { repository.getUser(id) }     //some operations, while user is being retrieved      val user = userJob.await() //here coroutine will be suspended for a while, and the method `await` is available only from `Deferred` interface     //do the job with retrieved user } Also, it is possible to structure this async request with an existing scope, but that is a talk of another question.
On a basic level, Deferred is a future. It makes it possible for one coroutine to wait for the result produced by another one, suspending itself until it's ready. Calling async is one way, but by far not the only way, to get a Deferred.
However, I think your question is more about the basics: when to use launch, when to use async-await. Here's an important lesson: you probably don't need async. People tend to use it because the keywords async and await are familiar from other languages, but in Kotlin, async is not a general-purpose tool to achieve non-blocking calls.
Here's a basic recipe on how to turn a blocking call into a suspending, non-blocking one:
uiScope.launch {     val ioResult = withContext(Dispatchers.IO) { blockingIOCall() }     ... just use the result, you're on the GUI thread here. } If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With