ݺߣ

ݺߣShare a Scribd company logo
Reactive X
An API for asynchronous programming with observable streams
Jieyi Wu
2017.01.31
part1
Introduction
Introduction
Usage
Scenario
Outline
Reactive X
Asynchronies
Observer
Pattern
Streaming
Characters
Observable Observer
Subscribe
(action)
Observable Observer
onError
onNext
onCompleted
Publisher
Difference between
streaming
list.stream()
.skip(2)
.filter(i -> 0 == (Integer)i % 2)
.map(integer -> integer.toString())
.reduce((s, s2) -> s.toString() + s2.toString())
.toString();
Observable.just(0, 1, 2, 3, 5, 6, 8, 10, 12)
.skip(2)
.filter(i -> 0 == i % 2)
.map(integer -> integer.toString())
.scan((s, s2) -> s + s2)
.subscribe(out -> System.out.println(out));
ArrayList list = new ArrayList<Integer>(0, 1, 2, 3, 5, 6, 8, 10, 12);
Usage
Introduction
Scenario
Outline
? And
? Then
? Merge
? Zip
Placeholder
? First
? Skip
? Filter
? Take
? Map
? FlatMap
? Scan
? GroupBy
Transforming
Filtering Combining
Transforming Operation
Map
Observable.just(1, 2, 3)
.map(index->String.valueOf(index+ 3))
.subscribe(s->System.out.println(s));
Output
4
5
6
Transforming Operation
Scan
Output
1
3
6
Observable.just(1, 2, 3)
.scan((i1,i2) ->i1 + i2)
.subscribe(out->System.out.println(out));
Filtering Operation
Filter
Output
1
3
5
Observable.just(1, 2, 3, 4, 5)
.filter(i->0 !=i% 2)
.subscribe(out->System.out.println(out));
Filtering Operation
Take
Output
4
5
6
Output
1
2
3
Observable.just(1, 2, 3, 4, 5)
.take(3)
.subscribe(out->System.out.println(out));
Filtering Operation
Skip
Output
4
5
Observable.just(1, 2, 3, 4, 5)
.skip(3)
.subscribe(out->System.out.println(out));
Combining Operation
Merge
Output
1
2
3
4
5
Observable.merge(
Observable.just(1, 2)
.subscribeOn(Schedulers.newThread()),
Observable.just(3, 4, 5)
.subscribeOn(Schedulers.newThread()))
.subscribe(out->System.out.println(out));
Combining Operation
Zip
Output
13
24
Observable.zip(
Observable.just(1, 2),
Observable.just(3, 4, 5),
(i1,i2) ->i1.toString() +i2.toString())
.subscribe(out->System.out.println(out));
Combining Operation
Concat
Output
1
2
3
4
5
Observable.concat(
Observable.just(1, 2),
Observable.just(3, 4, 5))
.subscribe(out->System.out.println(out))
Thread
Observable.just(0, 1, 2, 3, 5, 6, 8, 10, 12)
.skip(2)
.filter(i -> 0 != i % 2)
.subscribeOn(Schedulers.io())
.map(i -> i.toString())
.observeOn(Schedulers.computation())
.scan((s, s2) -> s + s2)
.observeOn(Schedulers.newThread())
.subscribe(out -> System.out.println(out));
in IO thread
in computation thread
in a new thread
}
2, 3, 4, 6, 8, 10, 12
2, 6, 8, 10, 12
2, 6, 8, 10, 12
2 6
26 8
268 10
26810 12
2
26
268
26810
2681012
Scenario
Introduction
Usage
Outline
Multi-clicking
RxView.clickable(view)
.debounce(500,TimeUnit.MILLISECONDS)
.observerOn(AndroidSchedulers.main())
.subscrib(o -> {
// What you wanna do.
});
Search keyword
RxSearchObservable.fromView(searchView)
.debounce(300, TimeUnit.MILLISECONDS)
.filter(text -> !text.isEmpty())
.distinctUntilChanged()
.switchMap(query -> dataFromNetwork(query))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> textViewResult.setText(result));
THANKS
Any Question?
To be continue...

More Related Content

Reactive X introduction part1