Group Group Group Group Group Group Group Group Group

Apply Coroutines to Retrofit |

This is a companion discussion topic for the original entry at

Thanks for this great tutorial. I need little guidance I have scenario that I want to run 2 calls together in parent job and I want to cancel the parent of all other childs if any one of child get failed or get an error. SuperVisorJob is not for this kind of usecase?Can you guide me for this

Hey @navsingh637!

From what I understand, you want every single job you launch to be cancelled, if any of the two main/important jobs are cancelled or get an error?

In that case, you can use one supervisor job for all the “less-important” jobs, and the two main/important jobs would be independant from that supervisor job. You can add the onCompletion callback to the job, to do some extra work, after either of the two main jobs completes.

mainJob1.invokeOnCompletion { cause -> 
  if(cause is CancellationException) supervisorJob.cancelChildren() // if cancelled. Also check for any other errors
mainJob2.invokeOnCompletion { cause ->  ... }

This could be a possible solution. Your use case is very specific, so I am unsure what other options you could use, but I feel like this is a good way to handle the flow.

You can check out the invokeOnCompletion documentation at the following link:

Hope this helps! :]

hey @filbabic my case is like this I have 2 tasks

  1. Get User profile
  2. Get user additional data

So I want to implement like [Get User Profile] [Get user additional data] both calls concurrently with asynch…
It’s working fine because both the calls triggered concurrently…but what I want to add is that if any one of this network call gets fail, I want to cancel the second one…
Reason can be anything [timeout, network failure…] or if they both success then proceed

Hey @navsingh637

Well, this should be taken care of out of the box. If you’re using built-in support from Retrofit, it should have cancellation implemented, and if those two tasks have the same parent, as soon as one of the requests fails, when you await, the entire parent-child hierarchy will be canceled.

Simply have those two async blocks under the same parent, and await after you call them, coroutines should take care of the rest.

@Filip Babić Hi I just finished the course on android coroutines In depth but I have a doubt!
At the end I just got confused when we implemented the coroutine with retrofit. so by default in the presenter our context is in the main and while making the network call if we arent switching the context isnt calls happing in the main thread?

Hey @prasen267 !

I explained in that video in the end, how Retrofit switches the context for you . So you don’t have to worry about it being on the main thread.

As soon as you put in suspend , the Http client underneath uses Dispatchers.IO, and does their own switching! It’s actually even better knowing a 3rd party will always do their job on switching to necessary threads, than you having to think about it.

As that way it’s both abstract, and safe. It’s much more likely that you’ll mess up if you have to do it yourself , than for a library to mess up, if it’s their responsibility, and they clearly express they did good work on the matter at hand.

Thanks for asking this!