Any Observable<T> is a streaming sequence of T objects. So if a method returned an Observable<Price> think of it as a stream of Prices. Observable : a collection of future values.
the pull-based, synchronous Iterable. The Observable contract : zero to N data events can happen, followed by a complete event. or an error event can happen at any time, also completing the Observable.
After an Observer calls an Observable's subscribe method, the Observable calls the Observer's onNext(T) method to provide notifications. Observable will call an Observer's onCompleted() method exactly once or the Observer's onError(java.lang. Throwable) method exactly once.
you connect an observer to an Observable. onNext : An Observable calls this method whenever the Observable emits an item. onError : Indicates failure to generate the expected data or some other error. Stops the Observable and will not make further calls to onNext or onCompleted. onCompleted : Called after onNext for the final time, if it has not encountered any errors.
Observable<Data> source = Observable.concat( sources.memory(), sources.disk(), sources.network() ) .first(data -> data != null && data.isUpToDate()); // "Request" latest data once a second Observable.interval(1, TimeUnit.SECONDS) .flatMap(__ -> source) .subscribe(data -> System.out.println("Received: " + data.value)); // Occasionally clear memory (as if app restarted) so that we must go to disk Observable.interval(3, TimeUnit.SECONDS) .subscribe(__ -> sources.clearMemory());
a subscription, observing or producing notifications, on any thread you like. Control the concurrency model for: 1. The invocation of the subscription 2. The observing of notifications locationService.getLocationsNearby(lon, lat, maxDistance, limit) .map(x -> x.location) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); observable .subscribeOn(Schedulers.newThread()) .unsubscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread());
an Observer. When to use a subject : A source observable can be one of two kinds: external and local : An external source is any observable or event that already exists outside of your code. A local source is when you generate an observable from your code. 1. If the source is external, then do not use a subject. 2. If the source is local, then do use a subject.
begins by emitting the item most recently emitted by the source Observable (or a seed/default value if none has yet been emitted) and then continues to emit any other items emitted later by the source Observable(s).
RxJava in several respects: • by changing the set of default computation, i/o, and new thread Schedulers • by registering a handler for extraordinary errors that RxJava may encounter • by registering functions that can take note of the occurrence of several regular RxJava activities Examples: • RxJavaSchedulersHook • RxJavaErrorHandler