RxJava vs TokenAuthenticator

What is RxJava?

A stream of events using the observer pattern

Hot Observables

Cold Observables

emit whether you are ready or not

emit when subscribed to

emit whether you are ready or not

Examples:

  • mouse and keyboard events
  • system events

Examples:​

  • db request
  • web request

Fits nicely when managed properly

 

Easy to handle real-time streams

Subjects

Observables
----------------
Disposables

Threading

Issues

Not disposing subscriptions leads to memory leaks

Android can kill your app at anytime - what happens with subjects then

If multiple observers subscribe to a given observable, how do you handle concurrency?

Not knowing RX operators can lead to a real mess reinventing the wheel over and over again

OkHttp

Can easily construct OkHttpClient instances of already existing ones

Shares an in-memory map to different routes as long as the base instance remains the same

Most used in Android, HTTP/2 support, connection pools, caching, GZIP encoding etc.

TokenAuthenticator in OkHttp

Refresh mechanism

When a 401 is received - refresh the token  by doing a synchronized call and retry the request

Current issues

AuthLibrary is from stone age - using AsyncTasks, not updated for a long time (only docs)

Audio player can use OkHttp but currently, when used, it works slow and buggy

Not easy to see how many API classes we have and make sure they also use the same shared OkHttp instance

Future

OkHttp everywhere

Proper architecture to manage RxJava disposables and avoid memory leaks

Remove subjects unless really necessary

Move away from RxJava to coroutines

Replace Auth library with the Android built-in

Thank you!

RxJava vs TokenAuthenticator

By Georgi Mirchev

RxJava vs TokenAuthenticator

  • 345