Search code examples
androidrx-javaintentserviceandroid-cursorloaderrx-android

rxJava and retrofit instead of IntentService + CursorLoader?


In my app I use IntentServices every time I do a network call. I use Volley for handling the HTTP communication and the results are stored in a database (using ContentProvider). In my Activities/Fragments I use CursorLoaders to update my UI components. This works great in case of Device rotations and all my network code is decoupled from my Activities/Fragments.

But at this moment my app has 30 database tables. I can predict that the number of tables will only increase. It feels messy and a bit too complicated.

I have been reading about rxJava / rxAndroid and Retrofit and it seems that many developers prefers rxJava instead of IntentServices+CursorLoaders. All the rxJava examples I have seen does not include IntentServices at all. Is it possible to use rxJava inside an IntentService that posts the result back to an Activity? (I can predict that some of the results returned from the API needs to be stored in a database)

How should I deal with device rotation if I dont use an IntentService?


Solution

  • Is it possible to use rxJava inside an IntentService that posts the result back to an Activity?

    You can use the standar Android components BroadcastReceiver, LocalBroadcastManager or an EventBus.

    Another ways, assuming the data displayed in the activity is loaded from the database:

    Use SQLBrite, a Rx wrapper around SQLiteDatabase to load the data in the activity. This works similarly to CursorLoader. When the data in the db changes it will reload the data.

    In your IntentService download the data and insert it in the db (using SQLBrite). SQLBrite will be notified and it will reload the data in the activity.