learn you some rx for the greater good

67
Artur Glier

Upload: 3camp

Post on 16-Jul-2015

151 views

Category:

Technology


6 download

TRANSCRIPT

Page 1: Learn you some rx for the greater good

Artur Glier

Page 2: Learn you some rx for the greater good

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM

Page 3: Learn you some rx for the greater good
Page 4: Learn you some rx for the greater good
Page 5: Learn you some rx for the greater good

✓ Microsoft Reactive Extensions by Erik Meijer

Fast forward

Page 6: Learn you some rx for the greater good

✓ Microsoft Reactive Extensions by Erik Meijer✓ Netflix RxJava & RxJs by Ben Christensen

Fast forward

Page 7: Learn you some rx for the greater good

✓ Microsoft Reactive Extensions by Erik Meijer✓ Netflix RxJava & RxJs by Ben Christensen✓ Github ReactiveCocoa

Fast forward

Page 8: Learn you some rx for the greater good

✓ Microsoft Reactive Extensions by Erik Meijer✓ Netflix RxJava & RxJs by Ben Christensen✓ Github ReactiveCocoa (Think different...)

Fast forward

Page 9: Learn you some rx for the greater good

Some of early adopters

Page 10: Learn you some rx for the greater good

RxJava is all about FRP

Page 11: Learn you some rx for the greater good

✓ Programming with asynchronous data streams

FRP is...

Page 12: Learn you some rx for the greater good

✓ Programming with asynchronous data streams

+✓ Toolbox

FRP is...

Page 13: Learn you some rx for the greater good

✓ Cross platform ;)✓ A fun new tool. “If you only have a hammer -

you tend to see each problem as nail.”✓ Less error prone✓ ! a steep learning curve

FRP is...

Page 14: Learn you some rx for the greater good

@interface RACSignal (Operations)

- (RACSignal *)doNext:(void (^)(id x))block;

- (RACSignal *)doError:(void (^)(NSError *error))block;

- (RACSignal *)doCompleted:(void (^)(void))block;

- (RACSignal *)throttle:(NSTimeInterval)interval;

- (RACSignal *)throttle:(NSTimeInterval)interval valuesPassingTest:(BOOL (^)(id next))predicate;

- (RACSignal *)delay:(NSTimeInterval)interval;

- (RACSignal *)repeat;

- (RACSignal *)initially:(void (^)(void))block;

- (RACSignal *)finally:(void (^)(void))block;

- (RACSignal *)bufferWithTime:(NSTimeInterval)interval onScheduler:(RACScheduler *)scheduler;

- (RACSignal *)collect;

- (RACSignal *)takeLast:(NSUInteger)count;

- (RACSignal *)combineLatestWith:(RACSignal *)signal;

+ (RACSignal *)combineLatest:(id<NSFastEnumeration>)signals;

+ (RACSignal *)combineLatest:(id<NSFastEnumeration>)signals reduce:(id (^)())reduceBlock;

- (RACSignal *)merge:(RACSignal *)signal;

+ (RACSignal *)merge:(id<NSFastEnumeration>)signals;

- (RACSignal *)flatten:(NSUInteger)maxConcurrent;

...

@end

Page 15: Learn you some rx for the greater good

RxJava is an extension of the observer desing pattern

Page 16: Learn you some rx for the greater good
Page 17: Learn you some rx for the greater good
Page 18: Learn you some rx for the greater good

� It’s pull-based approach and any logic requires the result to be available at execution time

Iterables

Page 19: Learn you some rx for the greater good

� Futures are straight-forward to use for a single level of asynchronous execution

� Conditional asynchronous execution flows become difficult to optimally compose

Futures

Page 20: Learn you some rx for the greater good

� They are easy to use with a single level of asynchronous execution but become unwieldy with nested composition.

Callbacks

Page 21: Learn you some rx for the greater good

� Producer can signal consumer that there is no more data available

� Producer can signal consumer that error has ocurred

Observer Pattern

Page 22: Learn you some rx for the greater good

How do you learn RxJava?

Page 23: Learn you some rx for the greater good

BY example

Page 24: Learn you some rx for the greater good

RxJava is all about FRP

Page 25: Learn you some rx for the greater good

FRP is...✓ Responsive, Reslient,

Elastic, Message Driven “FRP manifesto”

✓ Rx = Observables + LINQ + Schedulers “Microsoft”

✓ Programming paradigm for reactive programming using the building blocks of functional programming “Wikipedia”

✓ … “stackoverflow”

Page 26: Learn you some rx for the greater good

FRP is...✓ Responsive, Reslient,

Elastic, Message Driven “FRP manifesto”

✓ Rx = Observables + LINQ + Schedulers “Microsoft”

✓ Programming paradigm for reactive programming using the building blocks of functional programming “Wikipedia”

✓ … “stackoverflow”

Page 27: Learn you some rx for the greater good

FRP is...✓ Responsive, Reslient,

Elastic, Message Driven “FRP manifesto”

✓ Rx = Observables + LINQ + Schedulers “Microsoft”

✓ Programming paradigm for reactive programming using the building blocks of functional programming “Wikipedia”

✓ … “stackoverflow”

Page 28: Learn you some rx for the greater good

FRP is...✓ Responsive, Reslient,

Elastic, Message Driven “FRP manifesto”

✓ Rx = Observables + LINQ + Schedulers “Microsoft”

✓ Programming paradigm for reactive programming using the building blocks of functional programming “Wikipedia”

✓ … “stackoverflow”

Page 29: Learn you some rx for the greater good

FRP is...✓ Responsive, Reslient,

Elastic, Message Driven “FRP manifesto”

✓ Rx = Observables + LINQ + Schedulers “Microsoft”

✓ Programming paradigm for reactive programming using the building blocks of functional programming “Wikipedia”

✓ … “stackoverflow”

Bla, bla, bla...

Page 30: Learn you some rx for the greater good

Rx.Observable.prototype.flatMapLatest(selector, [thisArg])Projects each element of an observable sequence into a new

sequence of observable sequences by incorporating the element's

index and then transforms an observable sequence of observable

sequences into an observable sequence producing values only from

the most recent observable sequence.

Page 31: Learn you some rx for the greater good
Page 32: Learn you some rx for the greater good

BY example

Page 33: Learn you some rx for the greater good

RxJava is all about marbles

Page 34: Learn you some rx for the greater good

Not this kind of marbles

Page 35: Learn you some rx for the greater good

This kind of marbles

Page 36: Learn you some rx for the greater good

RxJava is all about code

Page 37: Learn you some rx for the greater good

Observable<String> observable = Observable.just("Hello, World!");

Page 38: Learn you some rx for the greater good

Subscriber<String> subscriber = new Subscriber<String>() {

@Override

public void onCompleted() {

// code here

}

@Override

public void onError(Throwable e) {

// code here

}

@Override

public void onNext(String s) {

output.append(String.format("%s\n", s));

}

};

Page 39: Learn you some rx for the greater good

@Override

protected void onStart() {

super.onStart();

observable

.observeOn(Schedulers.newThread())

.subscribeOn(AndroidSchedulers.mainThread())

.subscribe(subscriber);

}

Page 40: Learn you some rx for the greater good
Page 41: Learn you some rx for the greater good
Page 42: Learn you some rx for the greater good
Page 43: Learn you some rx for the greater good

Request compositionpublic interface AftonbladetAPI {

@GET("/item/{id}.json")

Observable<Item> item(@Path("id") long id);

@GET("/topstories.json")

Observable<List<Long>> topStories();

}

Page 44: Learn you some rx for the greater good

Request compositionGET /topstories

[ 8414149, 8414078, 8413972, 8411638, 8414102, 8413204, 8413100, 8413971,

8412744, 8414003, 8412841, 8412802, 8412605, 8413548, 8413123, 8414437,

8412897, 8413028, 8413341, 8412425, 8411762, 8413623, 8412346, 8411356,

8413056, 8413365, 8412372, 8414055, 8412877, 8412167, 8413264, 8414137,

8410519, 8412933, 8411846, 8412929, 8411254, 8411512, 8412777, 8412626,

8413274, 8414389, 8414117, 8412114, 8412212, 8412759, 8412696, 8412768,

8411643, 8411866, 8413966, 8410976, 8410545, 8410358, 8413979, 8414129,

8411791, 8409075, 8410314, 8411532, 8411553, 8412099, 8412085, 8410356,

8409084, 8412862, 8409823, 8412705, 8410220, 8409323, 8414090, 8410326 ]

Page 45: Learn you some rx for the greater good

Request compositionGET item/8863.json{

"by": "Daniel Söderbäck",

"id": 1,

"score": 111,

"time": 1415793648,

"title": "Swede Jacobson won the Poker World Championships",

"type": "story",

"url": "http://www.aftonbladet.se/sportbladet/poker/article19844615.ab"

}

Page 46: Learn you some rx for the greater good

Request compositionpublic void onRefresh() {

Aftonbladet.API.topStories()

.flatMap(longs -> Observable.from(longs))

.flatMap(id -> Aftonbladet.API.item(id))

.collect(new ArrayList<Item>(), (items, item) -> items.add(item))

.subscribeOn(Schedulers.newThread())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(...);

}

Page 47: Learn you some rx for the greater good

Request composition - flatMap()public void onRefresh() {

Aftonbladet.API.topStories()

.flatMap(longs -> Observable.from(longs))

.flatMap(id -> Aftonbladet.API.item(id))

.collect(new ArrayList<Item>(), (items, item) -> items.add(item))

.subscribeOn(Schedulers.newThread())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(...);

}

Page 48: Learn you some rx for the greater good

Request composition - flatMap()public void onRefresh() {

Aftonbladet.API.topStories()

.flatMap(longs -> Observable.from(longs))

.flatMap(id -> Aftonbladet.API.item(id))

.collect(new ArrayList<Item>(), (items, item) -> items.add(item))

.subscribeOn(Schedulers.newThread())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(...);

}

Page 49: Learn you some rx for the greater good

Request composition - collect()public void onRefresh() {

Aftonbladet.API.topStories()

.flatMap(longs -> Observable.from(longs))

.flatMap(id -> Aftonbladet.API.item(id))

.collect(new ArrayList<Item>(), (items, item) -> items.add(item))

.subscribeOn(Schedulers.newThread())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(...);

}

Page 50: Learn you some rx for the greater good

Request composition - threadspublic void onRefresh() {

Aftonbladet.API.topStories()

.flatMap(longs -> Observable.from(longs))

.flatMap(id -> Aftonbladet.API.item(id))

.collect(new ArrayList<Item>(), (items, item) -> items.add(item))

.subscribeOn(Schedulers.newThread())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(...);

}

Page 51: Learn you some rx for the greater good

Request compositionpublic void onRefresh() {

Aftonbladet.API.topStories()

.flatMap(longs -> Observable.from(longs))

.flatMap(id -> Aftonbladet.API.item(id))

.collect(new ArrayList<Item>(), (items, item) -> items.add(item))

.subscribeOn(Schedulers.newThread())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(...);

}

Page 52: Learn you some rx for the greater good

Request composition.subscribe(

items -> list.setAdapter(new ArrayAdapter<Item>(..., ..., items)),

error -> handleError(error),

() -> {

if (ptr.isRefreshing()) ptr.setRefreshing(false);

}

)

Page 53: Learn you some rx for the greater good
Page 54: Learn you some rx for the greater good

Request composition - filter()public void onRefresh() {

HackerNews.API.topStories()

.flatMap(longs -> Observable.from(longs))

.flatMap(id -> HackerNews.API.item(id))

.filter(item -> item.time > 1412985600)

.collect(new ArrayList<Item>(), (items, item) -> items.add(item))

.subscribeOn(Schedulers.newThread())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(...);

}

Page 55: Learn you some rx for the greater good
Page 56: Learn you some rx for the greater good

Request composition - limit()public void onRefresh() {

HackerNews.API.topStories()

.flatMap(longs -> Observable.from(longs))

.flatMap(id -> HackerNews.API.item(id))

.filter(item -> item.time > 1412985600)

.limit(10)

.collect(new ArrayList<Item>(), (items, item) -> items.add(item))

.subscribeOn(Schedulers.newThread())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(...);

}

Page 57: Learn you some rx for the greater good
Page 58: Learn you some rx for the greater good

Request compositionObservable.zip(

service.getUserPhoto(id),

service.getPhotoMetadata(id),

(photo, metadata) -> createPhotoWithData(photo, metadata))

.subscribe(photoWithData -> showPhoto(photoWithData));

Page 59: Learn you some rx for the greater good
Page 60: Learn you some rx for the greater good

public static Observable<String> getRegIdObservable(final Context context) {

return Observable.create(new Observable.OnSubscribe<String>() {

@Override

public void call(Subscriber<? super String> subscriber) {

try {

subscriber.onNext(getDeviceRegId(context));

subscriber.onCompleted();

} catch (Exception e) {

subscriber.onError(e);

}

}

});

}

Page 61: Learn you some rx for the greater good

GcmUtils.getRegIdObservable(this)

.subscribeOn(Schedulers.newThread())

.observeOn(Schedulers.newThread())

.subscribe(new Action1<Topic>() {

@Override

public void call(Topic topic) {

API.subscribeToTopic(/* ... */);

}

});

Page 62: Learn you some rx for the greater good
Page 63: Learn you some rx for the greater good

RxJava is all about fun

Page 65: Learn you some rx for the greater good

Questions?

[email protected]

Page 66: Learn you some rx for the greater good
Page 67: Learn you some rx for the greater good

Refactoring Android to RxJava