-
Notifications
You must be signed in to change notification settings - Fork 2
How To Use
Requisite first example which creates an Observable from a list of Strings, subscribes to the Observable with a function that will print "Hello [arg]!" for each string.
This example is given first in Java and then other languages to provide comparison.
Subsequent examples will use a mixture of languages all of which can be found in the rxjava-examples submodule.
public static void hello(String... names) {
Observable.toObservable(names).subscribe(new Action1<String>() {
@Override
public void call(String s) {
System.out.println("Hello " + s + "!");
}
});
}
hello("Ben", "George");
Hello Ben!
Hello George!
def hello(String[] names) {
Observable.toObservable(names)
.subscribe({ println "Hello " + it + "!"})
}
hello("Ben", "George")
Hello Ben!
Hello George!
(defn hello
[&rest]
(-> (Observable/toObservable &rest)
(.subscribe #(println (str "Hello " % "!")))))
(hello ["Ben" "George"])
Hello Ben!
Hello George!
An Observable sequence originates from two sources, an existing data structure or an Observable implementation which synchronously or asynchronously executes and passes data via onNext()
.
The Observable toObservable
, from
and just
methods allow converting any object, list or array of objects into an observable sequence:
Observable<Integer> o = Observable.toObservable(1, 2, 3, 4, 5, 6);
Observable<String> o = Observable.from("a", "b", "c");
def list = [5, 6, 7, 8]
Observable<Integer> o = Observable.toObservable(list);
Observable<String> o = Observable.just("one object");
These sequences will synchronously invoke onNext()
on an Observer when subscribed to for each object and then call onCompleted()
.
Asynchronous IO or computational operations or "infinite" streams of data can be implemented using the Observable class.
This can be done either by extending the Observable class or by using the Observable.create()
factory method.
More information can be found on the Observable and Creation Operators pages.
OnError subscribe, resume next, delay
A Netflix Original Production
Tech Blog | Twitter @NetflixOSS | Twitter @RxJava | Jobs