Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saveBufferoos is called even when data is retrieved from cache #15

Open
joreilly opened this issue Dec 27, 2017 · 2 comments
Open

saveBufferoos is called even when data is retrieved from cache #15

joreilly opened this issue Dec 27, 2017 · 2 comments

Comments

@joreilly
Copy link

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
Copy link
Author

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
Copy link
Author

joreilly commented Jan 1, 2018

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant