0
votes

When I create a new Observer as anonymous type It works Fine:

Observable<List<Post>> postsListObservable = mApplicationAPI.getPosts();

        postsListObservable.
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).subscribe( new Observer<List<Post>>() {

            @Override
            public void onSubscribe(Disposable d) {
                Log.i("ZOKa", "onSubscribe: ");
            }

            @Override
            public void onNext(List<Post> posts) {

                Log.i("ZOKa", "onNext: " + posts.size());
            }

            @Override
            public void onError(Throwable e) {
                Log.i("ZOKa", "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                Log.i("ZOKa", "onComplete: ");

            }
        });

When I create the Observer as a Dynamic Type it doesn't emit data

Observable<List<Post>> postsListObservable = mApplicationAPI.getPosts();

    postsListObservable.
            subscribeOn(Schedulers.io()).
            observeOn(AndroidSchedulers.mainThread());

    Observer<List<Post>>  observer = new Observer<List<Post>>() {

        @Override
        public void onSubscribe(Disposable d) {
            Log.i("ZOKa", "onSubscribe: ");
        }

        @Override
        public void onNext(List<Post> posts) {

            Log.i("ZOKa", "onNext: " + posts.size());
        }

        @Override
        public void onError(Throwable e) {
            Log.i("ZOKa", "onError: " + e.getMessage());
        }

        @Override
        public void onComplete() {
            Log.i("ZOKa", "onComplete: ");

        }
    };
    postsListObservable.subscribe(observer);

Logcat for the first code snippet:

com.tripleService.basesetupfordi/I/ZOKa: onSubscribe: com.tripleService.basesetupfordi/I/ZOKa: onNext: 100: com.tripleService.basesetupfordi/I/ZOKa: onComplete:

Logcat for the second one:

com.tripleService.basesetupfordi/I/ZOKa: onError: null

So, What is the diff in between?

1

1 Answers

1
votes

That's because Operators return new observables, but they don't modify the observable that they were called on. subscribeOn and observeOn in the second example has no impact on the postsListObservable and the observer.

Following should work:

Observable<List<Post>> postsListObservable = mApplicationAPI.getPosts();


    Observable<List<Post>> postsListObservable2 = postsListObservable.
            subscribeOn(Schedulers.io()).
            observeOn(AndroidSchedulers.mainThread());

    Observer<List<Post>>  observer = new Observer<List<Post>>() {
        ...
    };

    postsListObservable2.subscribe(observer);

or

Observable<List<Post>> postsListObservable = mApplicationAPI.getPosts();

    Observer<List<Post>>  observer = new Observer<List<Post>>() {
        ...
    };

    postsListObservable.
            subscribeOn(Schedulers.io()).
            observeOn(AndroidSchedulers.mainThread()).subscribe(observer);