reactive programming in android

18
Reactive Programming in Android ALDRIN SEYCHELL 1

Upload: aldrin-seychell

Post on 14-Aug-2015

80 views

Category:

Documents


4 download

TRANSCRIPT

1

Reactive Programming in AndroidALDRIN SEYCHELL

2

Modern ComputingAND MOBILE PHONES …

3

4

What’s reactive programming?◦ Data Flows◦ Asynchronous◦ Event-driven◦ Abstraction from concerns

◦ Low Level Threading◦ Synchonization◦ Side effects

5

Side effects have Latency◦ Reading a file◦ Calling an API◦ Data Processing◦ Etc …

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

11

12

13

14

Android Primer (Layouts)

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!

16

Demo