2016-07-26 23 views
0

我想使用領域中的緩存數據,然後使用改進來更新來自服務器的數據。我設法通過如下:使用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的resuslts來自 ? (改造或領域)

回答

0

我結束了同是編輯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"); 
     } 
    } 
} 

,但我相信,還有比這

更好的和更清潔的解決方案