reactive programming in android
Post on 14-Aug-2015
82 Views
Preview:
TRANSCRIPT
4
What’s reactive programming?◦ Data Flows◦ Asynchronous◦ Event-driven◦ Abstraction from concerns
◦ Low Level Threading◦ Synchonization◦ Side effects
6
Effects of ProgrammingSingle Many
Synchronous T getData() Iterable<T> getData()
Asynchronous Future<T> getData() Observable<T> getData()
7
Why Reactive Programming?◦ Reacting asynchronously to user interactions
◦ E.g. Text Input, Clicks on a button
◦ Composition of operations on stream of events◦ E.g. Accumulating clicks and/or text input changes
◦ Achieving non-blocking async operations◦ Abstraction to reason on different threads◦ Loading images into bitmaps
◦ Auto-completion with throttling◦ Avoid overloading of server
8
Observablespublic class Observable<T> {
Subscription subscribe(Subscriber<T> subscriber);...
}
public class Subscriber<T> {void onCompleted();void onError(Throwable e);void onNext(T t);
}
Asynchronous stream of events
Used to unsubscribe
9
Rx in Android (RxAndroid)◦ Reactive UI Components
◦ Such as TextViews, Buttons
◦ Easy to reason on UI Thread◦ Using Android Schedulers
◦ https://github.com/ReactiveX/RxAndroid◦ Based on RxJava
10
Composing Observables◦ Transform: map, flatMap, reduce, scan◦ Filter: take, filter, skip, sample, debounce◦ Combine: concat, merge, zip◦ Concurrency: observeOn, subscribeOn◦ Error Handling: onErrorReturn
Similar to Java 8/Scala collections but items except results are emitted when they arrive
15
Android Primer (Activity)◦ Has a layout◦ Holds the logic associated with a screen◦ Has its own lifecycle◦ Can update its own views
◦ Only on the main thread!
17
References◦ https://github.com/ReactiveX/RxAndroid◦ http://plainoldobjects.com/presentations/decomposing-applications-for-deployability-and-
scalability/futures-and-rx-observables-powerful-abstractions-for-consuming-web-services-asynchronously/
◦ https://github.com/kaushikgopal/Android-RxJava◦ http://www.slideshare.net/Tomik/reactive-programming-on-android?related=1
top related