what is hide method in class Observable used for? I read the document but still has no idea what it is used for and I saw lots of people use it
hide()
Hides the identity of this Observable and its Disposable.
http://reactivex.io/RxJava/javadoc/io/reactivex/Observable.html
When should we use this method?
Observables are used when we have relatively few items over the time and there is no risk of overflooding consumers. If there is a possibility that the consumer can be overflooded, then we use Flowable. One example could be getting a huge amount of data from a sensor. They typically push out data at a high rate.
onNext(): This method is called when a new item is emitted from the Observable. onError(): This method is called when an error occurs and the emission of data is not successfully completed. onComplete(): This method is called when the Observable has successfully completed emitting all items.
Completable is only concerned with execution completion whether the task has reach to completion or some error has occurred. interface CompletableObserver<T> { void onSubscribe(Disposable d); void onComplete(); void onError(Throwable error);
An Observable is analogous to a speaker that broadcasts the value. It does various tasks and generates some values. An Operator is similar to a translator in that it converts/modifies data from one form to another. An Observer is what fetches the value.
If you look at the documentation, you will see in the next sentence:
Allows hiding extra features such as Subject's Observer methods or preventing certain identity-based optimizations (fusion).
An example would be:
PublishSubject<Object> objectPublishSubject = PublishSubject.create();
Observable<Object> hide = objectPublishSubject.hide();
Lets say, you use a PublishSubject internally and you want to pass an Observable to the outside world. This would be a good idea, because of information hiding. The caller from outside would not be able to invoke #onNext() on an Observable. So, you could just use Observable as the return value of the method and just return the PublishSubject. That would be possible, but the caller would be able to cast it and would be able to invoke #onNext() from outside.
Observable#hide create a new Observable from PublishSubject, so no casting would be possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With