我參考github上的項目編寫我自己的代碼 https://github.com/AlbertGrobas/Leaderboards 但仍然發現一些問題。 在類的LeaderboardDataService「:Realm + RXJava + Retrofit從網絡或本地獲取數據
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。 該類是領域的模板類,用於提取數據的翻新。
問題是如何讓寫領域功能知道 數據來自領域或改造,以避免無用的操作。
我用的境界找到所有對象得到RealmResults,並將其轉換爲postsRealmList,看起來isValid返回false – zjywill