Search code examples
androidrx-javasqlbrite

RxJava data flow with SqlBrite and Retrofit


In my Android app I am using domain level Repository interface, which is backed with local DB implemented using SqlBrite and network api with Retrofit observables. So I have method getDomains(): Observable<List<Domain>> in Repository and two corresponding methods in my Retrofit and SqlBrite. I don't want to concatenate or merge, or amb these two observables. I want my Repository to take data only from SqlBrite and since SqlBrite returns QueryObservable, which triggers onNext() every time underlying data changed, I can run my network request independently and store results to SqlBrite and have my Observable updated with fetched from network and stored to DB data. So I tried to implement my Repository's getDomains() method as follow:

fun getDomains(): Observable<List<Domain>> {
    return db.getDomains()
                   .doOnSubscribe {
                       networkClient.getDomains()
                                    .doOnNext { db.putDomains(it) }
                                    .onErrorReturn{ emptyList() }
                                    .subscribe()
                   }
}

But in this case every time the client should subscribe, every time it would make network requests, that is not so good. I thought about other do... operators to move requests there, but doOnCompleted() in case of QueryObservable would never be called, until I call toBlocking() somewhere, which I won't, doOnEach() also not good as it makes requests every time item from db extracted. I also tried to use replay() operator, but though the Observable cached in this case, the subscription happens and results in network requests. So, how can combine these two Observables in the desired way?


Solution

  • Ok, it depends on the concrete use case you have: i.e. assuming you want to display the latest data from your local database and from time to time update the database by doing a network request in the background.

    Maybe there is a better way, but maybe you could do something like this

     fun <T> createDataAwareObservable(databaseQuery: Observable<T>): Observable<T> =
          stateDeterminer.getState().flatMap {
            when (it) {
              State.UP_TO_DATE -> databaseQuery // Nothing to do, data is up to date so observable can be returned directly
    
              State.NO_DATA ->
                networkClient.getDomains() // no data so first do the network call
                    .flatMap { db.save(it) } // save network call result in database
                    .flatMap { databaseQuery } // continue with original observable
    
              State.SYNC_IN_BACKGROUND -> {
                // Execute sync in background
                networkClient.getDomains()
                    .flatMap { db.save(it) }
                    .observeOn(backgroundSyncScheduler)
                    .subscribeOn(backgroundSyncScheduler)
                    .subscribe({}, { Timber.e(it, "Error when starting background sync") }, {})
    
                // Continue with original observable in parallel, network call will then update database and thanks to sqlbrite databaseQuery will be update automatically
                databaseQuery
              }
            }
          }
    

    So at the end you create your SQLBrite Observable (QueryObservable) and pass it into the createDataAwareObservable() function. Than it will ensure that it loads the data from network if no data is here, otherwise it will check if the data should be updated in background (will save it into database, which then will update the SQLBrite QueryObservable automatically) or if the data is up to date.

    Basically you can use it like this:

    createDataAwareObservable( db.getAllDomains() ).subscribe(...)
    

    So for you as user of this createDataAwareObservable() you always get the same type Observable<T> back as you pass in as parameter. So essentially it seems that you were always subscribing to db.getAllDomains() ...