использование Rxjava с модификацией и областью

я хочу использовать кешированные данные в области, а затем обновить данные с сервера, используя модификацию. мне удалось это следующим образом:

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<>();
                    }
                }
            });
}

моя проблема состоит в том, чтобы получить текущий статус, например: 1- если в области нет данных, показать прогресс 2- если нет данных и нет диалогового окна с ошибкой отображения сети 3- если есть данные в области, а сеть не показывает данные из области только 4 - если в области нет данных и нет данных от модернизации, не отображается состояние данных

Любая идея, как узнать, откуда получены результаты concat? (модернизация или царство)


person mahmoud    schedule 26.07.2016    source источник


Ответы (1)


в итоге я отредактировал метод 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");
        }
    }
}

но я считаю, что есть лучшее и более чистое решение, чем это

person mahmoud    schedule 26.07.2016