clean-architecture-components-boilerplate icon indicating copy to clipboard operation
clean-architecture-components-boilerplate copied to clipboard

saveBufferoos is called even when data is retrieved from cache

Open joreilly opened this issue 7 years ago • 2 comments

In the following code saveBufferoos is called even when data is retrieved from cache (db)....for relatively large amounts of data this can be pretty time consuming.

    override fun getBufferoos(): Flowable<List<Bufferoo>> {
        return factory.retrieveCacheDataStore().isCached()
                .flatMapPublisher {
                    factory.retrieveDataStore(it).getBufferoos()
                }
                .flatMap {
                    Flowable.just(it.map { bufferooMapper.mapFromEntity(it) })
                }
                .flatMap {
                    saveBufferoos(it).toSingle { it }.toFlowable()
                }
    }

joreilly avatar Dec 27 '17 18:12 joreilly

Following is a variation that will only save when remote data is fetched (though is missing right now check for cache being invalid)


    override fun getBufferoos(): Flowable<List<Bufferoo>> {
        return Flowable.concatArray(getBufferoosFromCache(), getBufferoosFromRemote())
                .firstElement()
                .toFlowable()
    }

    fun getBufferoosFromCache(): Flowable<List<Bufferoo>> {
        return factory.retrieveCacheDataStore().getBufferoos()
                .filter { it.isNotEmpty() }
    }

    fun getBufferoosFromRemote(): Flowable<List<Bufferoo>> {
        return factory.retrieveRemoteDataStore().getBufferoos()
                .flatMap {
                    saveBufferoos(it).toSingle { it }.toFlowable()
                }
    }

joreilly avatar Dec 28 '17 13:12 joreilly

What I've ended up using in my own code is Single source of truth approach where higher layers always subscribe to db updates (though still through repository interface) and any remote fetches will just update db (which in turn triggers update.....have modified queries in Room DAO to return a Flowable)

joreilly avatar Jan 01 '18 14:01 joreilly