environment • Configuration changes • Unreliable networking • Broadcasts • User Input A constant flow of changes and events has to be dispatched and handled while staying responsive and resilient.
sources of data • Observers: Classes that enable you to listen to those sources • Operators: Methods for modifying and composing streams and data CREATE STREAM LISTEN TO STREAM MODIFY/ COMBINE STREAM
sources of data • Observers: Classes that enable you to listen to those sources • Operators: Methods for modifying and composing streams and data Flowable<T> and Observable<T> • the source of our data • synchronous or asynchronous • 0, 1 or n items can be emitted • terminates never or by calling onComplete() or onError()
sources of data • Observers: Classes that enable you to listen to those sources • Operators: Methods for modifying and composing streams and data Flowable<T>: Handles backpressure, subscribe with: public void subscribe(Subscriber<T>); Observable<T>: No backpressure, subscribe with: public void subscribe(Observer<T>); Backpressure is the reactive way to deal with a common problem: An Observable emits items too fast for the Observer to consume it.
sources of data • Observers: Classes that enable you to listen to those sources • Operators: Methods for modifying and composing streams and data Subscribe to a Flowable by implementing: public interface Subscriber<T> { public void onSubscribe(Subscription s); public void onNext(T t); public void onError(Throwable t); public void onComplete(); } Subscribe to an Observable by implementing: public interface Observer<T> { public void onSubscribe(Disposable d); public void onNext(T t); public void onError(Throwable t); public void onComplete(); }
manipulate data • manipulate threads (schedulers) • manipulate emission of data RxJava, pants down! • Observables: Classes representing different types of sources of data • Observers: Classes that enable you to listen to those sources • Operators: Methods for modifying and composing streams and data
of sources: Maybe, Single and Completable Maybe.fromRunnable(() -> System. out.println("Hello World")); Maybe.fromAction(() -> System. out.println("Hello World")); Completable.fromRunnable(() -> System. out.println("Completed!")); Completable.fromAction(() -> System. out.println("Completed!")); Completable: onComplete() or onError() will be called. Maybe: onSuccess(T t) or onComplete() or onError() Single: onSuccess(T t) or onError() will be called.
"[email protected]")); userObservable.subscribe( user -> helloTextView.setText( "Hi" + user.nickname + "!"); ); Problems: • Potentially long running network operation! • We may not need the result anymore • Memory leaks
"[email protected]")); userObservable.subscribe( user -> helloTextView.setText( "Hi" + user.nickname + "!"); ); Problems: • Potentially long running network operation! • We may not need the result anymore • Memory leaks • NetworkOnMainThreadException
+ "!"; ); observeOn(Schedulers.io()): Networking will run in a background thread taken from the Thread pool of Schedulers.io() subscribeOn(AndroidSchedulers.mainThread()): All work that the Observer does will be done on the MainThread
"Obama", "Trump", "w", "Tr"); Observable.fromIterable(searchTerms) // Filter the search terms .filter(s -> s.length() > 2) // Create new SearchRequests from those search terms .flatMap(s -> Observable. just(new SearchRequest(s , time))) // Reduce the results to a single StringBuilder object .reduce(new StringBuilder() , (stringBuilder , searchRequest) -> stringBuilder.append(searchRequest.result())) // Print out the result .subscribe(System. out::print, e -> e.printStackTrace()) ;
.filter((clicks) -> clicks.size() > 0)) .subscribe((count) -> System. out.println(count + " clicks/s" )); Do not forget to dispose as there’s a strong reference to clickButton.
open source effort to collect good samples for RxJava usecases) • Instant/Auto searching text listeners (using Subjects & debounce) • Two-way data binding (using PublishSubject) • Polling (using interval and repeatWhen) • Exponential backoff (using delay and retryWhen) • Eventbus (using RxRelay and debounceBuffer) • Orchestrating Observable: make parallel network calls, then combine the result into a single data point (using flatmap & zip) …. https://github.com/kaushikgopal/RxJava-Android-Samples
a%20on%20Airbnb%20Android_0.pdf • Exploring RxJava 2 for Android • Jake Wharton https://www.youtube.com/watch?v=htIXKI5gOQU • Functional Reactive Programming with RxJava • Ben Christensen https://www.youtube.com/watch?v=_t06LRX0DV0&list=PLMacYHge0ewNw-VoFwXbNs9vCyvJumGnV • Learning RxJava for Android by example https://github.com/kaushikgopal/RxJava-Android-Samples • Fragmented Podcast: 004: The RxJava show with Dan Lew (Part 2) http://fragmentedpodcast.com/episodes/4/ • Reactivex.io: Documentation (Marbles!) http://reactivex.io/documentation/