ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Retrofit met Coroutines

--

Finally the latest version of Retrofit (2.6.0) has got out. While it was already really easy to use and so much fun, Retrofit is now easy, fun and shorter to write. Let’s deal with a refactor scenario from the previous version:

Version 2.5.0:

Obviously we have to return the same type until we await() for that response:

Notice we are returning the Deferredtype which holds our response. We won’t stop on what a Deffered is apart from sayin that it’s just a Future.

And when we want to get that response:

Notice we are calling the await() method in order to wait until our asynchronous operation has finished.

Well, no need for that anymore!

Version 2.6.0:

Drop the Deferred type and mark your methods as suspend . Again we wont stop on what suspend methods are apart from saying that it is a way to tell my program that this method is going to be a little late.

And after that, do the same until you await the response:

Now let’s implement the method:

Notice there is no need to await() that method because retrofit does that for you. It will return your defined type.

Note: You should also drop the .addCallAdapterFactory(CoroutineCallAdapterFactory()) from your Retrofit instance build.

Source:

Good luck.

You can find me on my personal blog:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Responses (3)

Write a response