SlideShare ist ein Scribd-Unternehmen logo
1 von 76
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Ähnlich wie RxSwift Training

379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogramming
Luis Atencio
 

Ähnlich wie RxSwift Training (20)

ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
 
Reactive programming
Reactive programmingReactive programming
Reactive programming
 
RxSwift
RxSwiftRxSwift
RxSwift
 
Reactive programming with scala and akka
Reactive programming with scala and akkaReactive programming with scala and akka
Reactive programming with scala and akka
 
Observables in Angular
Observables in AngularObservables in Angular
Observables in Angular
 
Reactive cocoa 101
Reactive cocoa 101Reactive cocoa 101
Reactive cocoa 101
 
RxSwift for Beginners - how to avoid a headache of reactive programming
RxSwift for Beginners - how to avoid a headache of reactive programmingRxSwift for Beginners - how to avoid a headache of reactive programming
RxSwift for Beginners - how to avoid a headache of reactive programming
 
379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogramming
 
Introduction to Functional Reactive Web with Clojurescript
Introduction to Functional Reactive Web with ClojurescriptIntroduction to Functional Reactive Web with Clojurescript
Introduction to Functional Reactive Web with Clojurescript
 
Progscon 2017: Taming the wild fronteer - Adventures in Clojurescript
Progscon 2017: Taming the wild fronteer - Adventures in ClojurescriptProgscon 2017: Taming the wild fronteer - Adventures in Clojurescript
Progscon 2017: Taming the wild fronteer - Adventures in Clojurescript
 
FRP with Ractive and RxJS
FRP with Ractive and RxJSFRP with Ractive and RxJS
FRP with Ractive and RxJS
 
Asynchronous javascript
 Asynchronous javascript Asynchronous javascript
Asynchronous javascript
 
Reactive Programming
Reactive ProgrammingReactive Programming
Reactive Programming
 
Rx Swift
Rx SwiftRx Swift
Rx Swift
 
RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 Slides
 
RxJava@DAUG
RxJava@DAUGRxJava@DAUG
RxJava@DAUG
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
 
PHP Reactive Programming at Medan Tech Day 2018
PHP Reactive Programming at Medan Tech Day 2018PHP Reactive Programming at Medan Tech Day 2018
PHP Reactive Programming at Medan Tech Day 2018
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaReactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJava
 
Functional Reactive Programming
Functional Reactive ProgrammingFunctional Reactive Programming
Functional Reactive Programming
 

Kürzlich hochgeladen

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 

Kürzlich hochgeladen (20)

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 

RxSwift Training

  • 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
  • 4. Functional programmingFunctional Programming R x S w i f t functionalProgramming through combine different function to fetch result 組合不同的函數而得到想要的結果 是一種Coding的方式
  • 5. 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
  • 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 將可觀察的事件增加響應的動作,以訂閱或是綁定稱之
  • 8. Core R x S w i f t
  • 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
  • 10. Before Start R x S w i f t
  • 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 :
  • 12. Observable R x S w i f t
  • 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.
  • 22. Observer R x S w i f t
  • 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 .
  • 35. Disposable R x S w i f t
  • 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.
  • 38. Relays R x S w i f t
  • 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
  • 40. Sample RelaysSample Relays R x S w i f t - R e p l a y s functionalProgramming
  • 41. Schedulers R x S w i f t
  • 42. 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
  • 43. Multi threadsMulti threads R x S w i f t - S c h e d u l e r s functionalProgramming Rewrite with Rx
  • 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 .
  • 49. Operator R x S w i f t
  • 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
  • 58. FlatMa p Flatmap R x S w i f t - O p e r a t o r functionalProgramming
  • 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.
  • 60. FlatMapLate st FlatMapLatest R x S w i f t - O p e r a t o r functionalProgramming
  • 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
  • 63. Share Replay Share Replay R x S w i f t - O p e r a t o r functionalProgramming
  • 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
  • 67. Systems R x S w i f t
  • 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
  • 69. RxDataSourc e RxDataSource R x S w 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. 更 新 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方式
  • 75. functionalProgramming ReferenceReference R x S w i f t • 中文文 • 英文文 • FlatMap vs Map • Share • AboutRx
  • 76. Q & A