RxSwiftRxSwift Training
Course & Workshop
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
Rx FeatureRx Feature
R x S w i f t
functionalProgramming
Easy to maintain
Combine easily
Stable
Less stateful
Without leaks
Functional programmingFunctional Programming
R x S w i f t
functionalProgramming
through combine different function to fetch result
組合不同的函數而得到想要的結果
是一種Coding的方式
Reactive ProgrammingReactive Programming
R x S w i f t
functionalProgramming
Events in a sequence ( it could be sync or async)
Response to each event
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 !
以時間為指標的事件序列陣列
SubscribeSubscribe
R x S w i f t
functionalProgramming
let Observable to connect with Observer
It called subscription or binding
將可觀察的事件增加響應的動作,以訂閱或是綁定稱之
Core
R x S w i f t
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
Before Start
R x S w i f t
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 :
Observable
R x S w i f t
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 ->
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
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
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 .
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 .
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 .
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 .
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 .
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.
Observer
R x S w i f t
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
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)
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
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
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 :
Observable
& Observe
R x S w i f t
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 .
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)
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
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 .
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
.
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 .
Disposable
R x S w i f t
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 .
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.
Relays
R x S w i f t
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
Sample RelaysSample
Relays
R x S w i f t - R e p l a y s
functionalProgramming
Schedulers
R x S w i f t
Multi threadsMulti threads
R x S w i f t - S c h e d u l e r s
functionalProgramming
Control task in which thread
Qos reference here
Multi threadsMulti threads
R x S w i f t - S c h e d u l e r s
functionalProgramming
Rewrite with Rx
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
Error
handling
R x S w i f t
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
RetryRetry
R x S w i f t - E r r o r H a n d l i n g
functionalProgramming
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 .
Operator
R x S w i f t
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
JustJust
R x S w i f t - O p e r a t o r
functionalProgramming
Create the only Elements.
OfOf
R x S w i f t - O p e r a t o r
functionalProgramming
Create Elements
FromFrom
R x S w i f t - O p e r a t o r
functionalProgramming
Turn one Objects into a Observable
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 .
FilterFilter
R x S w i f t - O p e r a t o r
functionalProgramming
Filter elements in sequence
MapMap
R x S w i f t - O p e r a t o r
functionalProgramming
turn original elements to other form
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
FlatMa
p
Flatmap
R x S w i f t - O p e r a t o r
functionalProgramming
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.
FlatMapLate
st
FlatMapLatest
R x S w i f t - O p e r a t o r
functionalProgramming
ShareShare
R x S w i f t - O p e r a t o r
functionalProgramming
Share resources to prevents firing duplicated events
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
Share
Replay
Share Replay
R x S w i f t - O p e r a t o r
functionalProgramming
PublicPublic
R x S w i f t - O p e r a t o r
functionalProgramming
connect several observables , fire them when connected
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
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
Systems
R x S w i f t
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
RxDataSourc
e
RxDataSource
R x S w i f t - S y s t e m s
functionalProgramming
基 本 輸 入 畫 面 控 制
functionalProgramming
ScenariosScenarios
R x S w i f t
1.輸入用戶名稱 > a.密碼框是否可以使用 b.隱藏提示Label
2.密碼輸入是否有效 > a.位元大於6 b.隱藏密碼提示Label
3.當密碼跟使用者名稱都輸入且有效 > 再打開下一步
4.按下下一步按鈕以後 > 彈跳出alert
A P I 控 制
functionalProgramming
ScenariosScenarios
R x S w i f t
1.一個API完成以後再發另一個API
2.等待兩個API完以後在進行某件事情 (zip)
更 新 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
一 件 事 情 以 後 接 續 其 他 事 情
functionalProgramming
ScenariosScenarios
R x S w i f t
a ) 先發request
b ) 在寫 案
c ) 更新UI
P.S 使用 雙向Observable
M V V M r e f a c t o r
functionalProgramming
ScenariosScenarios
R x S w i f t
1.修改MVVM Binding方式
functionalProgramming
ReferenceReference
R x S w i f t
• 中文文
• 英文文
• FlatMap vs Map
• Share
• AboutRx
Q & A

RxSwift Training

  • 1.
  • 2.
    Why Rx?Why RX? Rx 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 Rx S w i f t functionalProgramming Easy to maintain Combine easily Stable Less stateful Without leaks
  • 4.
    Functional programmingFunctional Programming Rx S w i f t functionalProgramming through combine different function to fetch result 組合不同的函數而得到想要的結果 是一種Coding的方式
  • 5.
    Reactive ProgrammingReactive Programming Rx S w i f t functionalProgramming Events in a sequence ( it could be sync or async) Response to each event
  • 6.
    What is RxSwift?Whatis 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 Sw i f t functionalProgramming let Observable to connect with Observer It called subscription or binding 將可觀察的事件增加響應的動作,以訂閱或是綁定稱之
  • 8.
    Core R x Sw i f t
  • 9.
    R x Sw i f t functionalProgramming 1. Observable 2. Observe 3. Observable & Observe 4. Relays 5. Operator 6. Disposable 7. Scheduler 8. Error Handler
  • 10.
  • 11.
    FrameworksFrameworks R x Sw 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 :
  • 12.
  • 13.
    MonitorMonitor R x Sw 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 Sw 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 Sw 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 Sw 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 Sw 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 Sw 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 Sw 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 Sw 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 Rx 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.
  • 22.
  • 23.
    Event responseEvent response Rx 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 Rx 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 Sw 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 Sw 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 Sw 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 :
  • 28.
  • 29.
    Special ObjectsSpecial objects Rx 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 Rx 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 Rx 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 Rx 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 Rx 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 Rx 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 .
  • 35.
  • 36.
    Disposable objectDisposable objects Rx 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 & ColdObservablesHot & 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.
  • 38.
    Relays R x Sw i f t
  • 39.
    What is relaysWhatis 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
  • 40.
    Sample RelaysSample Relays R xS w i f t - R e p l a y s functionalProgramming
  • 41.
  • 42.
    Multi threadsMulti threads Rx S w i f t - S c h e d u l e r s functionalProgramming Control task in which thread Qos reference here
  • 43.
    Multi threadsMulti threads Rx S w i f t - S c h e d u l e r s functionalProgramming Rewrite with Rx
  • 44.
    Thread typesThread types Rx 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
  • 45.
  • 46.
    Catch ErrorsCatch Errors Rx 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 Sw i f t - E r r o r H a n d l i n g functionalProgramming
  • 48.
    catchCatch R x Sw 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 .
  • 49.
  • 50.
    How to decidean 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 Sw i f t - O p e r a t o r functionalProgramming Create the only Elements.
  • 52.
    OfOf R x Sw i f t - O p e r a t o r functionalProgramming Create Elements
  • 53.
    FromFrom R x Sw i f t - O p e r a t o r functionalProgramming Turn one Objects into a Observable
  • 54.
    EmptyEmpty R x Sw 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 Sw i f t - O p e r a t o r functionalProgramming Filter elements in sequence
  • 56.
    MapMap R x Sw i f t - O p e r a t o r functionalProgramming turn original elements to other form
  • 57.
    FlatMapFlatmap R x Sw 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
  • 58.
    FlatMa p Flatmap R x Sw i f t - O p e r a t o r functionalProgramming
  • 59.
    FlatMapLate st FlatMapLatest R x Sw i f t - O p e r a t o r functionalProgramming it disposes previous subscription when new subscription happens.
  • 60.
    FlatMapLate st FlatMapLatest R x Sw i f t - O p e r a t o r functionalProgramming
  • 61.
    ShareShare R x Sw i f t - O p e r a t o r functionalProgramming Share resources to prevents firing duplicated events
  • 62.
    Share ReplayShare Replay Rx 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
  • 63.
    Share Replay Share Replay R xS w i f t - O p e r a t o r functionalProgramming
  • 64.
    PublicPublic R x Sw i f t - O p e r a t o r functionalProgramming connect several observables , fire them when connected
  • 65.
    MergeMerge R x Sw 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 Sw 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
  • 67.
  • 68.
    Membe rs Member s R x Sw 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
  • 69.
    RxDataSourc e RxDataSource R x Sw i f t - S y s t e m s functionalProgramming
  • 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.
    更 新 UI 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 VM r e f a c t o r functionalProgramming ScenariosScenarios R x S w i f t 1.修改MVVM Binding方式
  • 75.
    functionalProgramming ReferenceReference R x Sw i f t • 中文文 • 英文文 • FlatMap vs Map • Share • AboutRx
  • 76.