I have a usecase where I need to get the data from cache and followed by updating cache with data from server. So that when I access cache next time it will have the updated data.
I tried the concat() and merge() operator for combining cache and network call but at client I need only the first value which makes the network call observable getting disposed as soon as cache value is emitted.
Below is the code i am using , Is there any better way to achieve this. Currently I am doing subscription at doOnSuccess method which is not the right way.
class DataKit{
var cachedData: Data? = null
fun getData(): Single<Data> {
return if(cachedData != null){
Single.just(cachedData)
.doOnSuccess {
getPinInfo()
.subscribeOn(schedulerProvider.io())
.subscribeBy(defaultErrorFun)
}
}else getPinInfo()
}
fun getPinInfo():Single<Data>{
api.getDataFromServer()
.doOnSuccess{ cachedData = it}
.doOnError{cachedData = null}
}