0

Given the following chain:

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

Which is invoked like this:

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

I find myself wondering why neither doOnNext(...) nor doOnError(...) is being invoked. The chain is being executed until toList(), thus the below lines are being executed, it just stops afterwards.

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

A breakpoint at poiCollectionById and another one inside findPoiCollectionById(...) clearly show, the result is being fetched from DB successfully!

So, what's preventing doOnNext(...) from being called? I clearly invoke subscribe(...) on the observable. The mapping chain runs until toList(). I never see the code run into doOnError(...), nor do I ever run into the Action<Throwable> part of subscribe(...). Must have something to do with toList().

John Smith
  • 752
  • 9
  • 35

1 Answers1

0

This is not an answer to my question, but a solution to my problem

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

I really wish I hadn't had to create the ArrayList, but rather do something like the collect(Collectors.toList()) method of Java 8 Stream API by utilizing rxJavas toList().

John Smith
  • 752
  • 9
  • 35