[英]using Rxjava with retrofit and realm
i want to use the cached data in realm then update the data from server using retrofit. 我想在领域中使用缓存的数据,然后使用改造从服务器更新数据。 i managed that by the following: 我通过以下方式进行管理:
public void getNotifications() {
Observable.concat(getCashedNotifications(), downloadNotification())
.subscribe(new Action1<List<Notification>>() {
@Override
public void call(List<Notification> notifications) {
setSize(notifications.size() + "");
}
});
}
private Observable<List<Notification>> getCashedNotifications() {
return Observable.just(mRealm.copyFromRealm(mRealm.where(Notification.class).findAll()));
}
private Observable<List<Notification>> downloadNotification() {
return mApiHandler.createRetrofitService(NotificationServices.class)
.getNotificationByUser(10)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(new Action1<NotificationResponse>() {
@Override
public void call(final NotificationResponse notificationResponse) {
setLoading(false);
mRealm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(notificationResponse.getResult().getData().getNotifications());
}
});
}
})
.map(new Func1<NotificationResponse, List<Notification>>() {
@Override
public List<Notification> call(NotificationResponse notificationResponse) {
if (notificationResponse.getResult() != null) {
return notificationResponse.getResult().getData().getNotifications();
} else {
return new ArrayList<>();
}
}
});
}
my problem is to get the current status like : 1- if there is no data in realm show progress 2- if there is no data and no network show error dialog 3- if there is data in realm and no network show the data from realm only 4- if there is no data in realm and no data from retrofit show no data state 我的问题是获取当前状态,例如:1-如果领域中没有数据显示进度2-如果没有数据并且没有网络显示错误对话框3-如果领域中有数据并且没有网络显示来自领域的数据仅4-如果领域中没有数据并且翻新中的数据也没有显示数据状态
any idea how to know the resuslts from concat are from ? 知道如何知道concat的撤消来自何处? (retrofit or realm) (改造或领域)
what i ended up with is to edit the getNotifications method to the following 我最终得到的是将getNotifications方法编辑为以下内容
public void getNotifications() {
setNoData(false);
setLoading(false);
if (ConectivityUtils.isDeviceConnectedToNetwork(mContext)) {
if (mRealm.where(Notification.class).count() > 0) {
Observable.concat(getCashedNotifications(), downloadNotification())
.subscribe(new Action1<List<Notification>>() {
@Override
public void call(List<Notification> notifications) {
setSize(notifications.size() + "");
}
});
} else {
// show progress
setLoading(true);
downloadNotification().subscribe(new Action1<List<Notification>>() {
@Override
public void call(List<Notification> notifications) {
setLoading(false);
if (notifications.size() > 0) {
setSize(notifications.size() + "");
} else {
// no data in realm and retrofit
setNoData(true);
setErrorMessage("No data");
}
}
});
}
} else {
if (mRealm.where(Notification.class).count() > 0) {
getCashedNotifications().subscribe(new Action1<List<Notification>>() {
@Override
public void call(List<Notification> notifications) {
setSize(notifications.size() + "");
}
});
} else {
//show no network
setNoData(true);
setErrorMessage("No Network");
}
}
}
but i believe that there is better and cleaner solution than this 但我相信有比这更好更好的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.