2. Why Rx?Why RX?
R x S w i f t
functionalProgramming
Easier to achieve target action
Convenient in delegation
Deal with async tasks , even with notification
Combine tasks which dependent on each other
3. Rx FeatureRx Feature
R x S w i f t
functionalProgramming
Easy to maintain
Combine easily
Stable
Less stateful
Without leaks
6. What is RxSwift?What is RxSwift
R x S w i f t
functionalProgramming
It’s an array of sequence , the Index would be Time ,
and the Item will be Event !
以時間為指標的事件序列陣列
7. SubscribeSubscribe
R x S w i f t
functionalProgramming
let Observable to connect with Observer
It called subscription or binding
將可觀察的事件增加響應的動作,以訂閱或是綁定稱之
9. R x S w i f t
functionalProgramming
1. Observable
2. Observe
3. Observable & Observe
4. Relays
5. Operator
6. Disposable
7. Scheduler
8. Error Handler
11. FrameworksFrameworks
R x S w i f t - O b s e r v a b l e
functionalProgramming
github "ReactiveX/RxSwift" ~> 5.0
github "RxSwiftCommunity/RxDataSources" ~> 4.0
RxSwift Link :
RxDataSource Link :
13. MonitorMonitor
R x S w i f t - O b s e r v a b l e
functionalProgramming
Every thing is sequence
It can be any thing ,
ex: temperature , humidity , episode of drama
Time ->
14. MonitorMonitor
R x S w i f t - O b s e r v a b l e
functionalProgramming
In App , textField change , Button taps , Switch
could be Observables
15. MonitorMonitor
R x S w i f t - O b s e r v a b l e
functionalProgramming
On Next > trigger an Event
On Error > generator an Error
On complete > finish observable , stop monitor it
If knows the feature of observables , choose the
observable which match with features
16. SingleSingle
R x S w i f t - O b s e r v a b l e
functionalProgramming
Only contains one event whether Success or Error .
It doesn`t effect with Share operator .
17. CompletableCompletable
R x S w i f t - O b s e r v a b l e
functionalProgramming
Only contains one event whether Complete or Error .
It doesn`t effect with Share operator .
18. MaybeMaybe
R x S w i f t - O b s e r v a b l e
functionalProgramming
Only contains one event whether Success , Complete
or Error . It doesn`t effect with Share operator .
19. DriverDriver
R x S w i f t - O b s e r v a b l e
functionalProgramming
Driver trigger events without Errors , and must run at
Main Threads .
It dose effects with Share operator .
20. SignalSignal
R x S w i f t - O b s e r v a b l e
functionalProgramming
Signal is just like Driver . But it will replay last event .
It dose effects with Share operator .
21. Control EventControl Event
R x S w i f t - O b s e r v a b l e
functionalProgramming
Control event is to describe UI components of events .
It must run and subscribe in Main thread . event
shouldn’t contains any error.
23. Event responseEvent response
R x S w i f t - O b s e r v e r
functionalProgramming
observer is use for monitoring events , and
responses it with actions
How to create an observer ?
1. subscribe
2. binding
Observable<Type> Actions
24. Any ObserverAny Observer
R x S w i f t - O b s e r v a b l e
functionalProgramming
It used to describe any kind of observer , you’ll be able to
define action in specific status ( next , error , complete)
25. BinderBinder
R x S w i f t - O b s e r v a b l e
functionalProgramming
It contains two features
1.do not need to handle errors
2.make sure run at specific thread
26. BinderBinder
R x S w i f t - O b s e r v a b l e
functionalProgramming
ex : we only care about next event at UI Observer,
so it is a good change to use binding
It works on Main Thread
27. Binder-ReuseBinder-Reuse
R x S w i f t - O b s e r v a b l e
functionalProgramming
If the observer is usually in used , that can be define
with an extension .
It can be define like :
29. Special ObjectsSpecial objects
R x S w i f t - O b s e r v a b l e & O b s e r v e r
functionalProgramming
Some of objects are specials . It can be define
in whether Observable or Observer .
30. Special ObjectsSpecial objects
R x S w i f t - O b s e r v a b l e & O b s e r v e r
functionalProgramming
Choose the subject which matches the feature !
It usually used to combine with two events .
1. Async Subject
2. Publish Subject
3. Replay Subject
4.Behavior Subject
5. Variable (deprecated)
31. Async SubjectAsync Subject
R x S w i f t - O b s e r v a b l e & O b s e r v e r
functionalProgramming
The subscriber receives only last object when
event has been completed .
note : it will not get any element value if the event is not completed
32. Publish subjectPublish Subject
R x S w i f t - O b s e r v a b l e & O b s e r v e r
functionalProgramming
send elements after subscription . It will not send
elements which emit before subscription.
note : write the character type which the object is .
33. Replay SubjectReplay Subject
R x S w i f t - O b s e r v a b l e & O b s e r v e r
functionalProgramming
send all elements no matter when subscribes
.
34. Behavior SubjectBehavior Subject
R x S w i f t - O b s e r v a b l e & O b s e r v e r
functionalProgramming
emit the newest elements . If there is not ,
emit the default value .
36. Disposable objectDisposable objects
R x S w i f t - D i s p o s a b l e
functionalProgramming
In common situation . The resource will release
when event calls onError() or onComplete() .
You may call dispose() to release the event .
Sometimes use DisposeBag
to manage life cycle just like ARC .
37. Hot & Cold ObservablesHot & Cold Observables
R x S w i f t - D i s p o s a b l e
functionalProgramming
reference here
• A “hot” Observable may begin emitting
items as soon as it is created.
• A “cold” Observable, on the other hand,
waits until an observer subscribes to it
before it begins to emit items.
39. What is relaysWhat is Relays
R x S w i f t - R e p l a y s
functionalProgramming
Relays provides two kinds
1.Publish Relays
2.Behavior Relays
Relays never Complete and never emit errors
Relays only emit next and never terminate
Relays and Subjects are Hot Observables
44. Thread typesThread types
R x S w i f t - S c h e d u l e r s
functionalProgramming
MainScheduler :
> Main Thread
SerialDispatchQueueScheduler :
> Serial of Dispatcher Queue
ConcurrentDispatchQueueScheduler :
> Concurrent of Dispatcher Queue
OperationQueueScheduler :
> Same features like NSOperationQueue
46. Catch ErrorsCatch Errors
R x S w i f t - E r r o r H a n d l i n g
functionalProgramming
Task will stop if any error be threw out . There
are two methods to prevent error occurs .
1. retry
2. catch
47. RetryRetry
R x S w i f t - E r r o r H a n d l i n g
functionalProgramming
48. catchCatch
R x S w i f t - E r r o r H a n d l i n g
functionalProgramming
When error occurs , replace with backup Item or Observable .
50. How to decide an operator ?How to decide an operator?
R x S w i f t - O p e r a t o r
functionalProgramming
Operators help to transform elements to
other type elements .
How to decide an
operator
51. JustJust
R x S w i f t - O p e r a t o r
functionalProgramming
Create the only Elements.
52. OfOf
R x S w i f t - O p e r a t o r
functionalProgramming
Create Elements
53. FromFrom
R x S w i f t - O p e r a t o r
functionalProgramming
Turn one Objects into a Observable
54. EmptyEmpty
R x S w i f t - O p e r a t o r
functionalProgramming
Create a empty Observable
It also uses in stop observable .
55. FilterFilter
R x S w i f t - O p e r a t o r
functionalProgramming
Filter elements in sequence
56. MapMap
R x S w i f t - O p e r a t o r
functionalProgramming
turn original elements to other form
57. FlatMapFlatmap
R x S w i f t - O p e r a t o r
functionalProgramming
transform the items emitted by an Observable into Observables
then flatten the emissions from those into a single Observable .
note : FlatMap operator doesn’t care about the order order
59. FlatMapLate
st
FlatMapLatest
R x S w i f t - O p e r a t o r
functionalProgramming
it disposes previous subscription when new
subscription happens.
61. ShareShare
R x S w i f t - O p e r a t o r
functionalProgramming
Share resources to prevents firing duplicated events
62. Share ReplayShare Replay
R x S w i f t - O p e r a t o r
functionalProgramming
Share replay with times
replay : 0 -> Public subject
replay : 1 -> Behavior subject
replay : n -> Replay subject
It will fire observable event again when subscriber
become zero
64. PublicPublic
R x S w i f t - O p e r a t o r
functionalProgramming
connect several observables , fire them when connected
65. MergeMerge
R x S w i f t - O p e r a t o r
functionalProgramming
merge several observables into one observable
It will be stopped when one of observables occurs any error
66. ZipZip
R x S w i f t - O p e r a t o r
functionalProgramming
Combines each index of observables (at least 8) ,
the length of emission observable will equals to the
shortest one
68. Membe
rs
Member
s
R x S w i f t - S y s t e m s
functionalProgramming
Just like RxCocoa provide UI with Rx ,
other system also provide frameworks with Rx
1.RxDataSource
2.RxGestures
3.RxAlamofire
…….
Other Rx Systems
70. 基 本 輸 入 畫 面 控 制
functionalProgramming
ScenariosScenarios
R x S w i f t
1.輸入用戶名稱 > a.密碼框是否可以使用 b.隱藏提示Label
2.密碼輸入是否有效 > a.位元大於6 b.隱藏密碼提示Label
3.當密碼跟使用者名稱都輸入且有效 > 再打開下一步
4.按下下一步按鈕以後 > 彈跳出alert
71. A P I 控 制
functionalProgramming
ScenariosScenarios
R x S w i f t
1.一個API完成以後再發另一個API
2.等待兩個API完以後在進行某件事情 (zip)
72. 更 新 U I T a b l e V i e w
functionalProgramming
ScenariosScenarios
R x S w i f t
a ) 先發request
b ) 轉換成List
c ) 更新TableUI
73. 一 件 事 情 以 後 接 續 其 他 事 情
functionalProgramming
ScenariosScenarios
R x S w i f t
a ) 先發request
b ) 在寫 案
c ) 更新UI
P.S 使用 雙向Observable
74. M V V M r e f a c t o r
functionalProgramming
ScenariosScenarios
R x S w i f t
1.修改MVVM Binding方式