场景
我有一个区域列表,我想从服务器获取每个区域中的所有项目。所以,我决定用它来试试。我不知道我是不是“疯了”。这就是我所做的:BehaviorSubject
private Observable<ItemListResponse> getItemByTerritory() {
List<String> territories = PrefUtils.getUserTerritories(context);
BehaviorSubject<Integer> territoryControl = BehaviorSubject.<Integer>create(0);
Observable<ItemListResponse> territoryRet = territoryControl.asObservable().concatMap(integer -> {
if (integer != territories.size()) {
BehaviorSubject<Integer> ret = BehaviorSubject.create(1);
return ret.asObservable().concatMap(integer1 -> {
if (integer > 0) {
return bakeryAPI.getBakeries("Bearer " + PrefUtils.getToken(context), integer1, 21, territories.get(integer))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(itemListRespont -> {
if (itemListRespont.getItemResponse().getMeta().getPage() != itemListRespont.getItemResponse().getMeta().getPageCount() && itemListRespont.getItemResponse().getMeta().getPageCount() > 0) {
ret.onNext(integer1 + 1);
} else {
ret.onNext(-1);
territoryControl.onNext(integer + 1);
}
});
} else {
return Observable.<ItemListResponse>empty().doOnCompleted(ret::onCompleted);
}
});
} else {
return Observable.<ItemListResponse>empty().doOnCompleted(territoryControl::onCompleted);
}
});
return Observable.defer(() -> territoryRet);
}
它所做的只是挂起,就像我这样做时根本没有运行
public void getItemByTerritory(APIRequestListener apiRequestListener){
realm.executeTransaction(realm1 -> realm1.where(RealmItem.class).findAll().deleteAllFromRealm());
getItemByTerritory().subscribe(new Subscriber< ItemListResponse >() {
@Override
public void onCompleted() {
Log.e(TAG, "Completed Bakery");
apiRequestListener.completed();
unsubscribe();
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
apiRequestListener.handleError(e);
}
@Override
public void onNext(ItemListResponse itemListRespont) {
}
});
}