2017-06-09 68 views
0

鑑於以下鏈:doOnNext()從來沒有所謂的

public Observable<List<PoiCollection>> findPoiCollectionsByUserId(Integer userId) { 
    return findUserGroupsByUserId(userId) 
      .flatMapIterable(
        userGroups -> userGroups) 
      .flatMap(
        userGroup -> findPoiCollectionToUserGroupsByUserGroupId(userGroup.getId())) 
      .flatMapIterable 
        (poiCollectionToUserGroups -> poiCollectionToUserGroups) 
      .flatMap(
        poiCollectionToUserGroup -> { 
         Observable<PoiCollection> poiCollectionById = findPoiCollectionById(poiCollectionToUserGroup.getPoiCollectionId()); 
         return poiCollectionById; 
        }) 
      .toList() 
      .doOnNext(poiCollections -> { 
       Timber.d("poi-collections from DB:", poiCollections); 
       for(PoiCollection collection : poiCollections) { 
        Timber.d("collection:", collection); 
       } 
      }) 
      .doOnError(throwable -> 
        Timber.e("error fetching poi-collections for user from DB")); 
} 

被調用是這樣的:

Observable<List<PoiCollection>> fromDB = databaseHelper.findPoiCollectionsByUserId(id); 

fromDB.subscribeOn(Schedulers.io()) 
     .observeOn(AndroidSchedulers.mainThread()) 
     .subscribe(
       poiCollections -> { 
        Activity activity = (Activity) getView(); 
          Intent intent = new Intent(activity, PoiCollectionsActivity.class); 
        intent.putExtra("poi_collections", (Serializable) poiCollections); 
        activity.startActivity(intent); 
        activity.finish(); 
       }, 
       throwable -> { 
        if (throwable instanceof SocketTimeoutException) { 
         getView().showInternetDialog(); 
        } 
       }); 

,我發現自己不知道爲什麼沒有doOnNext(...)也不doOnError(...)被調用。鏈正在執行,直到toList(),因此下面的行被執行,之後它才停止。

poiCollectionToUserGroup -> { 
      Observable<PoiCollection> poiCollectionById = findPoiCollectionById(poiCollectionToUserGroup.getPoiCollectionId()); 
      return poiCollectionById; 
     }) 

斷點在poiCollectionById,並從DB順利另一個內部findPoiCollectionById(...)清楚地表明,其結果,正在獲取!

那麼,什麼能阻止doOnNext(...)被調用?我明確調用subscribe(...)上的可觀察。映射鏈運行至toList()。我從來沒有看到代碼運行到doOnError(...),我也沒有碰到subscribe(...)Action<Throwable>部分。必須與toList()有關。

+0

訂閱時,你是否在onNext()中獲取物品? – Ritesh

+0

你可以把doOnNext()放在toList()之前並嘗試。 – Ritesh

+0

是的,我可以,我也很期待。問題是toList()調用。但是,我必須能夠創建一個列表。否則毫無意義。 –

回答

0

這不是一個回答我的問題,但我的問題

public Observable<List<PoiCollection>> findPoiCollectionsByUserId(Integer userId) { 

    List<PoiCollection> poiCollections = new ArrayList<>(); 

    findUserGroupsByUserId(userId) 
      .flatMap(Observable::from) 
      .flatMap(userGroup -> findPoiCollectionToUserGroupsByUserGroupId(userGroup.getId())) 
      .flatMap(Observable::from) 
      .flatMap(poiCollectionToUserGroup -> findPoiCollectionById(poiCollectionToUserGroup.getPoiCollectionId())) 
      .doOnNext(collection -> poiCollections.add(collection)) 
      .subscribe(); 

    return Observable.just(poiCollections); 
} 

的解決方案我真的希望我還沒有創建ArrayList,而是做一些像Java 8的collect(Collectors.toList())方法通過利用rxJavas toList()流API。

相關問題