-
Notifications
You must be signed in to change notification settings - Fork 13
/
RxJava
99 lines (73 loc) · 3.74 KB
/
RxJava
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
RxJava : https://www.youtube.com/playlist?list=PLXjbGq0ERjFq0KvT7clQoOxM5TTd03yji
## Observables
1- Observable: emit a stream elements (endlessly)
2- Single: emits exactly one element
3- Maybe: emits zero or one elements
4- Completable: emits a “complete” event, without emitting any data type, just a success/failure
5- Flowable: emit a stream of elements (endlessly, with backpressure to emitting huge numbers of values) and allows to control how fast a source emits items.
Flowable types:
- On Back Pressure drop: working on Task1 & receive T2, T3 >> Drop T2 & T3
- On Back pressure latest: working on Task1 & receive T2, T3 >> Drop T2 and keep T3
- On Back pressure buffer: working on Task1 & receive T2, T3 , T4 & buffer size 2 >> save T2 & T3 in buffer , Then we have 3 types to handle T4:
- On overflow error : Error
- On overflow drop latest : Drop T4
- On overflow drop oldest: Drop T3 & Put T4 in the buffer
Observable Types:
Example: Publisher: A B C D E F G
Cold: (Observable) only execute values when something subscribes to it. For each subscriber the Observable starts a new execution
First subscriber: A B C D E F G
Second subscriber: A B C D E F G
Hot: Emit values even if there's no subscribes
- Connectable Observable: For each subscriber the Observable observe the new emitted value ( pulish & connect)
First subscriber: A B C D - E F G
Second subscriber: E F G
- PublishSubject: For each subscriber the Observable observe the new emitted value
First subscriber: A B C D - E F G
Second subscriber: E F G
- BehaviourSubject: Listen to the latest
First subscriber: A B C D - E F G
Second subscriber: D E F G
- ReplaySubject: Pause the old and repeat for the new subscriber
First subscriber: A B C D E F G
Second subscriber: A B C D E F G
- AsyncSubject: Observe the latest
First subscriber: G
Second subscriber: G
## Observer
- Obserever / SingleObserver / CompletableObserver / MaybeObserver
Excution: observable.subscribe(observer)
--------------------------------
## Operators: Factories method / Creation Operator to create Observables
1 - Observable.intervalRang
2 - ObservableOnSubscribe: on Error it will not continue
3 - Observable.just : take 10 values
4 - Observable.fromArray
5 - Observable.rage
6 - Observable.timer
--------------------------------
##schedulers
- IO / Computation / newThread / mainThread / Trampoline (first in first out)
--------------------------------
Observable work on upStream
Observer work on downStream
Default it work on Main thread
Excution: observable.just(1 , 2, 3 , 4, 5)
.subscribeOn(Schedulers.io) >>> it will change upstream & down stream
.subscribe(observer)
Excution: observable.just(1 , 2, 3 , 4, 5)
.subscribeOn(Schedulers.io) >>> upstream work on io
.observeOn(Schedulers.main) >>> downstream work on main
.subscribe(observer)
--------------------------------
Operators
map: Transform one stream to another by Apply function on each element and return a single value for each element
flatMap: the same as map but FlatMap is used to map over asynchronous operations
debounce: wait for x time to emit downstream
distinctUntilChanged: emit the same value 2 times from the upstream -> observe only one time on the down stream
filter: filter based on method
switchMap: it will unsubscribe from previous observable every time it gets a new item that needs to be mapped
--------------------------------
Disposable
Observables --- Disposable (connection) --> Observer
CompositeDisposable.add( Disposable return from subscribe)
CompositeDisposable.clear