[英]Realm+RXJava+Retrofit fetch data from net or local
我在github上引用了該項目以編寫自己的代碼https://github.com/AlbertGrobas/Leaderboards但仍然發現了一些問題。 在“ LeaderboardDataService”類中:
public Observable<Leaderboard> getLeaderboard(final String bracket, final boolean forceUpdate) {
final Observable<RealmLeaderboard> apiObservable = mClient.getApiClient().leaderboardObservable(bracket, Locale.getDefault().toString());
final Observable<RealmLeaderboard> dbObservable = mDAO.readLeaderboard(mHost, bracket);
return dbObservable.exists(new Func1<RealmLeaderboard, Boolean>() {
@Override
@DebugLog
public Boolean call(RealmLeaderboard leaderboard) {
return (leaderboard != null);
}
}).flatMap(new Func1<Boolean, Observable<RealmLeaderboard>>() {
@Override
@DebugLog
public Observable<RealmLeaderboard> call(Boolean inDB) {
return (inDB && !forceUpdate) ? dbObservable : apiObservable;
}
}).flatMap(new Func1<RealmLeaderboard, Observable<RealmLeaderboard>>() {
@Override
@DebugLog
public Observable<RealmLeaderboard> call(RealmLeaderboard leaderboard) {
if (leaderboard.getHost() == null) {
leaderboard.setBracket(bracket);
leaderboard.setHost(mHost);
}
return mDAO.writeLeaderboard(leaderboard, forceUpdate);
}
}).map(new Func1<RealmLeaderboard, Leaderboard>() {
@Override
@DebugLog
public Leaderboard call(RealmLeaderboard leaderboard) {
return realmToLeaderboard(leaderboard);
}
}); }
此函數用於確定從何處獲取數據(本地或網絡)。 有兩個問題。 1.首先存在的功能是判斷數據是否存在於領域中。 只返回一個布爾值,如果領域獲取了數據,則需要再次查詢,如果領域為空,則從網上獲取數據是沒有問題的。 2.在下一步中,如果數據來自網絡,則可以更新現有數據,但是如果數據來自領域,則無需更新這些數據。 該操作對來自領域的數據毫無用處。
我對函數做了一些修改:
public Observable<List<ChhPost>> getPosts(final boolean forceUpdate) {
final String path = "getpostdata";
final Observable<RealmList<ChhPost>> apiObservable = getApiService().getApiClient().posts(path);
final Observable<RealmList<ChhPost>> dbObservable = getObservableDAO().readPosts();
return dbObservable.flatMap(new Func1<RealmList<ChhPost>, Observable<RealmList<ChhPost>>>() {
@Override
public Observable<RealmList<ChhPost>> call(final RealmList<ChhPost> chhPosts) {
boolean valid = chhPosts!=null && chhPosts.size()>0;
if(valid && !forceUpdate){
Observable<RealmList<ChhPost>> postsObservable = Observable.create(new Observable.OnSubscribe<RealmList<ChhPost>>() {
@Override
public void call(Subscriber<? super RealmList<ChhPost>> subscriber) {
subscriber.onNext(chhPosts);
subscriber.onCompleted();
}
});
return postsObservable;
}else{
return apiObservable;
}
}
}).flatMap(new Func1<RealmList<ChhPost>, Observable<RealmList<ChhPost>>>() {
@Override
public Observable<RealmList<ChhPost>> call(RealmList<ChhPost> topics) {
Loge.d("getPosts write to Db");
return mDAO.writePosts(topics, forceUpdate);
}
}).map(new Func1<RealmList<ChhPost>, List<ChhPost>>() {
@Override
public List<ChhPost> call(RealmList<ChhPost> topics) {
List<ChhPost> newTopics = new ArrayList<>();
for (ChhPost topic : topics) {
newTopics.add(topic);
}
return newTopics;
}
});
}
第一個問題似乎已經解決,但第二個問題仍然存在。 做過一些研究,Rx onNext函數只能有一個參數。 在這種情況下,我傳遞了我自己的類的RealmList。 該類是用於領域和改造以獲取數據的s模板類。
問題是如何讓寫入域功能知道數據來自域還是改版,以避免無用的操作。
您可以使用RealmList.isValid()
,如果數據不是由Realm管理的,它將返回false: https : RealmList.isValid()
當前的Javadoc有點錯誤,我們將修復此問題:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.