Observable
methods such as onComplete()
and onError()
. These methods are called at the moment when the Observable
stops generating new data, either because it has nothing more to generate, or because an error has occurred.Subscriber
followed onCompleted()
and onError()
. Let's do something useful at these points: Observable.just("Hello, world!") .map(s -> potentialException(s)) .map(s -> anotherPotentialException(s)) .subscribe(new Subscriber<String>() { @Override public void onNext(String s) { System.out.println(s); } @Override public void onCompleted() { System.out.println("Completed!"); } @Override public void onError(Throwable e) { System.out.println("Ouch!"); } });
potentialException()
and anotherPotentialException()
can throw exceptions during operation. Each Observable
terminates with a call to onCompleted()
or onError
. In this case, the output of the program will be either a line followed by “Completed!”, Or the output will consist of a single “Ouch!” (Because an exception was thrown).onError()
is called regardless of when the exception was thrown.Observables
chain becomes a Subscriber
task, because every exception should be directly in onError()
.Subscriber
stopped receiving new items.Observable
never completes its execution).Observable
should not even know what to do with errors! This also applies to operators: they will not be executed if at some of the previous stages we had a critical error. All error handling is in Subscriber
.View
state from outside the main thread.Observer
and Subscriber
should run, using, respectively, subscribeOn()
and observeOn()
: myObservableServices.retrieveImage(url) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(bitmap -> myImageView.setImageBitmap(bitmap));
Subscriber
is performed in a separate I / O stream, and the manipulations with View
are already working in the main stream 1 .subscribeOn()
and observeOn()
can be called on any Observable
, since they are only operators. There is no need to worry about what our Observable()
does, or the operators following it - you can simply add subscribeOn()
and observeOn()
at the very end in order to scatter tasks across the necessary streams.AsyncTask
, or something similar, we need to write code, taking into account which parts of it should be executed in parallel. In the case of RxJava, we simply write the code - and then we indicate where we should execute it 2 .Observable.subscribe()
, you are returned with an object of the Subscription
class, which is the connection between your Observable
and Subscriber
: Subscription subscription = Observable.just("Hello, World!") .subscribe(s -> System.out.println(s));
Subscription
received by us in order to terminate the subscription: subscription.unsubscribe(); System.out.println("Unsubscribed=" + subscription.isUnsubscribed()); // "Unsubscribed=true"
unsubscribe
will stop execution regardless of what code is being executed. 3 Nothing more is required.Subscriber
should be made as lightweight as possible: so as not to block the main stream more than necessary.observeOn()
and subscribeOn()
. For example, even if the Observable
promises to work for a long time, while the Subscriber
will be running on the I / O stream, there is no reason to transfer the latter to the new stream.Observable.just()
is not the same as the self-written Observable
, which calls onNext()
and onCompleted()
. And it's about the subscriptions: in the case of Observable.just()
before the onNext()
call, it checks whether Subscriber
is still signed or not.Source: https://habr.com/ru/post/265727/
All Articles