我正在尝试使用RxJava
的{{1}}运算符来实现优化的缓存和网络方法,以便从服务器获取数据。我正在使用Gson模型来解析来自API的JSON响应。
由于使用Gson模型,我无法从服务器检索数据。因为返回的类型与Netwrok请求和磁盘缓存不匹配。
我一直在测试几种方法,但没有成功地做到这一点。
ApiService.java
takeUntil
ineteractor.java
@GET(ApiConstants.GET_QUESTIONS_URL) Observable<RequestResponse> getQuestions();
interactor.java
public void performGetElQuestions(String query, QuestionsRequestServerCallback callback) {
getFreshNetworkData()//
.publish(network ->//
Observable.merge(network,//
getCachedDiskData().takeUntil(network)))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableObserver<Question>() {
@Override
public void onComplete() {
callback.onQuestionsReady(mQuestionsList);
}
@Override
public void onError(Throwable e) {
callback.onQuestionsFailed();
}
@Override
public void onNext(Question question) {
// mQuestionsList is an arraylist
mQuestionsList.add(question);
}
});
缓存数据
private Observable<Question> getFreshNetworkData() {
return apiService.getQuestions()
.flatMap(Observable::fromIterable)
.doOnSubscribe((data) -> new Handler(Looper.getMainLooper())//
.post(() -> adapterSubscriptionInfo.add("(network) subscribed")))//
.doOnComplete(() -> new Handler(Looper.getMainLooper())//
.post(() -> adapterSubscriptionInfo.add("(network) completed")));
}
Gson模型解析器
RequestResponse.java
private Observable<Question> getCachedDiskData() {
List<Question> list = new ArrayList<>();
//get cached data from SQLite or disk
return Observable.fromIterable(list)//
.doOnSubscribe((data) -> new Handler(Looper.getMainLooper())//
.post(() -> Timber.d("(disk) cache subscribed")))//
.doOnComplete(() -> new Handler(Looper.getMainLooper())//
.post(() -> Timber.d("(disk) cache completed")));
}
谢谢:)。
答案 0 :(得分:0)
我使用领域并使用RXJava concat + take(1)的优点,每次我检索数据并缓存它,但如果innet连接缓慢缓存数据总是准备好。 国家实体的代码示例:
public Observable<ArrayList<Pair<Country,City>>> queryAllAndCopyOrLoad(){
return Observable.concat(
Observable.just(storageInteractor.queryAllAndCopySync(Country.class))
.filter(RxPretty::notEmpty)
.subscribeOn(AndroidSchedulers.mainThread()),
networkService.serverApi()
.getCountries()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(countries -> storageInteractor.deleteAllAndPut(countries, Country.class))
.doOnNext(countries -> firstTime = false))
.take(1)
.flatMap(this::transformToCityCountryPair);
}