Entering the Rx world

Issues

Previously we were using a wrapper library above the old Play Services Location library, which exposed all the functionality to a reactive fashion by converting a PendingResult to an Observable.

Although, this library has not received any updates since 2017 and it had also not been migrated to the newest style of Location library, which uses Task instead of PendingResult.

Initial approach

Our initial approach was to fork the library and migrate it to use the newest Places SDK, but after the first proof-of-concept this turned to be a huge task and also quite bloated, since the parts we needed could be abstracted in something simpler (we will see below).

Solution

Since the burden of migrating the whole library was too much, we decided to convert the callback-style Task API, to a reactive way that would fit in our codebase and would produce the minimum changes needed.

The solution was pretty simple. All we needed to do is create a new class that would extend ObservableOnSubscribe.

TaskObservable.kt

This allowed us to create a simple extension function (we are using Kotlin, but this would be pretty trivial task in a Java codebase as well), to convert any Task to an Observable.

fun <T> Task<T>.toObservable(): Observable<T> = Observable.create(TaskObservable(this))

The last step was to remove the library we were using and replace the usage sites with our solution.

We should also notice here, that previously we were using the library as-is. After this change and since this is always a subject to change, the legacy part needed to be hidden behind an interface. This will save us time in the future, in case there are more changes to come and will make our testing easier.