onNext on Observer is never called

Asked

Viewed 47 times

0

Consider the following code snippet.

ReplaySubject<List<Object>> subject = ReplaySubject.create();

subject.subscribe(view::mostraListaObjetos);

subject.onNext(getListOnline());
//logo apos essa linha o metodo view.mostrarListaObjetos(lista)
//deveria ser chamado ou estou errado?

The problem is that no matter how many times I call onNext() the Observer never gets anything. someone has an idea of why?

Edited had summarized the code, but I found that this way works well, but when I use the operators flatMapInterableand toList is that I get nothing until the subject.onComplete() is called!

Example of detailed code.

ReplaySubject<List<Object>> subject = ReplaySubject.create();
subject.flatMapIterable(object -> {
            Log.d(TAG, "flatMapIterable: it is called");
            return object;
        }).doOnEach(objNotification -> Log.d(TAG, "doOnEach: it is called"))
          .toList()
          .subscribe(obj -> Log.d(TAG, "subscribe: it is only called after subscribe.onComplete()"));
subject.onNext(getListOfObjects());
Log.d(TAG, "onComplete:");
subject.onComplete();

Output from the logcat:

07-04 23:20:38.258 10770-10770/app.package D/TAG: flatMapIterable: it is called
07-04 23:20:38.259 10770-10770/app.package D/TAG: doOnEach: it is called
07-04 23:20:38.259 10770-10770/app.package D/TAG: onComplete:
07-04 23:20:38.260 10770-10770/app.package D/TAG: doOnEach: it is called
07-04 23:20:38.260 10770-10770/app.package D/TAG: it is only called after subscribe.onComplete()

1 answer

0


This is the standard behavior of the toList() operator, which causes some confusion. As explained in Javadoc, he needs the upstream signal the onComplete to issue the list.

In the diagram it is also possible to see this behavior, since the arrow only leaves the operator toList after the onComplete of upstream:

inserir a descrição da imagem aqui

  • Perfect. Thank you very much Leonardo!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.