[英]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.