RxJava - How Observable works

Observables represents the sources of data where as Observers (Subscribers) listen to them. In nutshell, an Observable emits items and a Subscriber then consumes these items

Observables represents the sources of data where as Observers (Subscribers) listen to them. In nutshell, an Observable emits items and a Subscriber then consumes these items

Observable

  • Observable provides data once subscriber starts listening.

  • Observable can emit any number of items.

  • Observable can emit only signal of completion as well with no item.

  • Observable can terminate successfully.

  • Observable may never terminate. e.g. a button can be clicked any number of times.

  • Observable may throw error at any point of time.

Subscriber

  • Observable can have multiple subscribers.

  • When an Observable emits an item, each subscriber onNext() method gets invoked.

  • When an Observable finished emitting items, each subscriber onComplete() method gets invoked.

  • If an Observable emits error, each subscriber onError() method gets invoked

 

Creating Observables

Observable<String> observer = Observable.just("Hello RxJava");

We have different ways to create observable

  • Flowable − 0..N flows, Emits 0 or n items. Supports Reactive-Streams and back-pressure.

  • Observable − 0..N flows ,but no back-pressure.

  • Single − 1 item or error. Can be treated as a reactive version of method call.

  • Completable − No item emitted. Used as a signal for completion or error. Can be treated as a reactive version of Runnable.

  • MayBe − Either No item or 1 item emitted. Can be treated as a reactive version of Optional.

Methods to create Observable

  • just(T item) − Returns an Observable that signals the given (constant reference) item and then completes.

  • fromIterable(Iterable source) − Converts an Iterable sequence into an ObservableSource that emits the items in the sequence.

  • fromArray(T... items) − Converts an Array into an ObservableSource that emits the items in the Array.

  • fromCallable(Callable supplier) − Returns an Observable that, when an observer subscribes to it, invokes a function you specify and then emits the value returned from that function.

  • fromFuture(Future future) − Converts a Future into an ObservableSource.

  • interval(long initialDelay, long period, TimeUnit unit) − Returns an Observable that emits a 0L after the initialDelay and ever increasing numbers after each period of time thereafter

public class RXJava {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        Observable<String> observer = Observable.just("Observable with Observable"); // provides datea
        observer.subscribe(s -> System.out.println(s));
        
        Flowable.just("Observable with Flowable ").subscribe(System.out::println);
     
        Single single=   Single.just("Observable with Single ");
        single.subscribe(System.out::println);
       
    }
    
}

 

Output

Observable with Observable
Observable with Flowable 
Observable with Single 

 

*

RxJava Tutorial RxJava - Environment Setup RxJava’s Characteristics RxJava - How Observable works RxJava - Single Observable RxJava - MayBe Observable RxJava - Completable Observable RxJava - Using CompositeDisposable RxJava - Creating Operators RxJava - Transforming Operators RxJava - Filtering Operators RxJava - Combining Operators RxJava - Utility Operators RxJava - Conditional Operators RxJava - Mathematical Operators RxJava - Subjects RxJava - PublishSubject RxJava - BehaviorSubject RxJava - AsyncSubject RxJava - ReplaySubject RxJava - Schedulers RxJava - Trampoline Scheduler RxJava - NewThread Scheduler RxJava - Computation Scheduler RxJava - IO Scheduler RxJava - From Scheduler RxJava - Buffering