-
-
Save egor-n/581c54db79bd1c1defed to your computer and use it in GitHub Desktop.
RxJava caching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private PublishSubject<Data> dataRequest; | |
private Data data; | |
// ... | |
Subscription getData(Observer<Data> observer) { | |
if (data != null) { | |
observer.onNext(data); | |
} | |
if (dataRequest != null) { | |
return dataRequest.subscribe(observer); | |
} | |
dataRequest = PublishSubject.create(); | |
Subscription subscription = dataRequest.subscribe(observer); | |
dataRequest.subscribe(new EndObserver<Data>() { | |
@Override | |
public void onEnd() { | |
dataRequest = null; | |
} | |
@Override | |
public void onNext(Data data1) { | |
data = data1; | |
} | |
}); | |
apiService.data() | |
.map(this::cache) | |
.subscribeOn(ioScheduler) | |
.observeOn(uiScheduler) | |
.subscribe(dataRequest); | |
return subscription; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment