A journey from Async Task to Kotlin Coroutines

A journey from Async Task to Kotlin CoroutinesMarco GomieroBlockedUnblockFollowFollowingJan 13Photo by felipe lopez on UnsplashSome weeks ago I released a new version of the RSS Parser Library and I talked about the update in a blog post.

RSS Parser 2.

0: bye bye Async Task, welcome CoroutinesFinally, I found some time to sit down and work on my library RSS Parser.

Before starting to describe all the cool…medium.

comThis update brought a huge change in the infrastructure of the library.

SPOILER: Kotlin and coroutines.

Today, in this post I want to talk about the transition process and all the decisions that I have made to develop this new version.

In this way, I hope to inspire you to leave the Async Task and get into the coroutines world.

But, before starting with the technical details, I want to share you some resources to get into the coroutine world.

If you already know the coroutines you can skip to the second part of the article.

Get into the coroutine world, it’s funny.

I promise:The first thing that you can do to get into the coroutine world is doing the codelab provided by Google.

Using Kotlin Coroutines in your Android AppBy the end of this codelab you will have enough experience to convert an existing API to use coroutines, and you will…codelabs.

developers.

google.

comDon’t worry if you don’t understand all the concepts, the codelab is useful to make the first exploration and to receive the inputs and the tools to study a particular argument.

After the codelab, I suggest you give a look to the official documentation that is well written and full of examples.

Kotlin/kotlinx.

coroutinesLibrary support for Kotlin coroutines .

Contribute to Kotlin/kotlinx.

coroutines development by creating an account on…github.

comThen you could read some articles Android specific and not.

Here are some articles that I’ve read:An introduction to Kotlin CoroutinesCoroutines that fascinated devs from its early beta have graduated the experimental phase and are now stable…antonis.

meHow to make sense of Kotlin coroutinesCoroutines are a great way to write asynchronous code that is perfectly readable and maintainable.

Kotlin provides the…proandroiddev.

comKotlin Coroutines patterns & anti-patternsDecided to write about several things which in my opinion you should and shouldn’t do (or at least try to avoid) when…proandroiddev.

comPlaying with Kotlin in Android: coroutines and how to get rid of the callback hellIn this post I will talk about how Kotlin coroutines can allow you to get rid of the callback hell in your Android code…medium.

comAndroid Networking with Coroutines and RetrofitRecently I’ve been reading up on, watching talks on and been generally getting more curious about Kotlin coroutines…medium.

comHandle Complex Network Call with Kotlin Coroutine + Retrofit 2When you’ve been working with some Android applications, you may encounter the scenario where you have to make multiple…blog.

oozou.

comAsync code using Kotlin CoroutinesKotlin coroutines allow to write asynchronous code in a familiar way, the structure of the code doesn’t change if you…proandroiddev.

comI suggest also the talks of Chris Banes and Christina Lee:Android SuspendersSo you’ve read the Coroutines guide and you’re ready to start using them in your Android app to coroutines?.Great!chris.

banes.

meSkills Matterdroidcon London 2018 conference cast.

Christina Lee: Coroutines are not a new concept, but they are much abuzz in the…skillsmatter.

comOf course, there are lots of resources available and lots of ways to learn the coroutines.

These are some advice based on my experience and learning path.

The Path From Async Task to CoroutinesThe first release of the library is dated 18 June 2016, a period when there wasn’t all the beautiful stuff that there is today (for instance, Kotlin) and moreover I did not know all the stuff that I know today.

The code was so simple (and now I can also say that was ugly) but it was working.

Old School Java CodeI used an Async Task to handle the network request; the result of the request is sent to an XML Parser that notifies its result when the parsing was done.

Here’s the code of the Parser:Then, the result of the parsing (or an error of parsing) is notified to the “main executor” (the application that uses the library) with two simple callbacks.

Kotlin and Coroutines, a love storyAfter 2 years, I wanted to get rid of Async Task, Java and all the ugly stuff.

The perfect candidates for taking the place are Kotlin and the coroutines.

However, my biggest concern was maintaining the compatibility with all the devs that still use Java (seriously guys? Love yourself, move to Kotlin).

In fact, the Kotlin coroutines cannot be invoked from Java code.

At first, I tried to figure out if there was a method to call the coroutines from Java but finally I came up with a brilliant idea: provide the support for both the ways.

For the Java support, I decided to use Future and Callable to handle the asynchronous operations.

In particular, I implemented two classes that perform respectively the fetching and the parsing task.

The result of the parsing is then notified to the “main executor” using the same callbacks reported above.

In this way, the old users of the library can still call the same code without noticing any kind of difference but the new ones (and of course also the old) can learn and use the new way.

As you can image, the new part is written using the Kotlin coroutines.

As above, I separated the fetching and the parsing task.

The fetching task is performed by the fetchXML suspending function, that takes the URL of the RSS feed as input and returns a Deferred object that will be the input of the parseXML suspend function.

This function will then parse the RSS Feed and returns a list of parsed data.

These functions are exposed to the “main executor” by using another suspend function, that it will get and parse asynchronously the RSS feed.

All the suspend functions reported above are called with the IO Dispatcher that uses a shared pool of on-demand created threads.

There are also other dispatchers, give a look to the documentation to find the one that better suits your needs.

And finally, from the ViewModel (or in whatever place depending on the architecture of your app) you can launch the coroutine with a Scope, so, for example, you can stop it if the activity is destroyed, and then “transform” an URL to a List of Articles.

And finally, we have reached the end of my journey from Async Task to Coroutines.

If you have some advice, doubt or any kind of feedback, please leave a comment!.Of course, you can use this example as an idea to leave forever the (ugly) Async Tasks.

If you want to contribute to the development of the library or simply report a bug, visit the repo on Github: https://github.

com/prof18/RSS-ParserA special thanks to the (awesome) devs of the Android Developers Italia Community that gave to me some advice.

If you are Italian, join us on Slack!.

. More details

Leave a Reply