![Page 1: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/1.jpg)
FRP in Swift 3101
© Andy Jacobs, ixor.be 2016 1
![Page 2: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/2.jpg)
Andy Jacobs7 years iOS development2 years FRP experience
1.5 year FRP enthusiast
© Andy Jacobs, ixor.be 2016 2
![Page 3: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/3.jpg)
Topics• What?
• Why?
• Basic concepts
• Real world example
• Tip
© Andy Jacobs, ixor.be 2016 3
![Page 4: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/4.jpg)
What?streams of values over time
© Andy Jacobs, ixor.be 2016 4
![Page 5: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/5.jpg)
What?streams of values over time
+ operations and bindings
© Andy Jacobs, ixor.be 2016 5
![Page 6: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/6.jpg)
Streams
© Andy Jacobs, ixor.be 2016 6
![Page 7: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/7.jpg)
OperationsMost operators operate on a given stream and will return a stream. This allows you to apply these operators one after the other, in a chain.
Each operator in the chain modifies the stream that results from the operation of the previous operator.
© Andy Jacobs, ixor.be 2016 7
![Page 8: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/8.jpg)
Common Operators
// Creatingcreate, just, from, ..// Transformingmap, flatMap, scan, ..// Filteringfilter, skip, take, ..// CombiningcombineLatest, merge, zip, ..// Utilitysubscribe, delay, observeOn, ..// ConditionaltakeUntil, skipWhile, ..// Mathematical & Aggregateconcat, reduce, ..// Connectablepublish, replay, connect, ..
© Andy Jacobs, ixor.be 2016 8
![Page 9: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/9.jpg)
Bindingsbind the stream of values to an endpoint.
• Subjects • Variables• UI
© Andy Jacobs, ixor.be 2016 9
![Page 10: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/10.jpg)
Why?You might already be doing some reactive programming
var title: String { didSet { // react to changes in variables view.titleLabel.text = title }}
© Andy Jacobs, ixor.be 2016 10
![Page 11: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/11.jpg)
Heavy lifting
Libraries asRxSwift, ReactiveCocoa, ReactKit, Bond, ..bring so much more to the table.
© Andy Jacobs, ixor.be 2016 11
![Page 12: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/12.jpg)
RxSwift, ReactiveX
In short, using Rx will make your code:Composable ⇠ Because Rx is composition's nicknameReusable ⇠ Because it's composableDeclarative ⇠ Because definitions are immutable and only data changesUnderstandable and concise ⇠ Raising the level of abstraction and removing transient statesStable ⇠ Because Rx code is thoroughly unit testedLess stateful ⇠ Because you are modeling applications as unidirectional data flowsWithout leaks ⇠ Because resource management is easy
© Andy Jacobs, ixor.be 2016 12
![Page 13: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/13.jpg)
Basic concepts• Observable
• Event
• Disposing
• Subjects
• Hot vs Cold
• Schedulers
© Andy Jacobs, ixor.be 2016 13
![Page 14: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/14.jpg)
Observable
Every Observable sequence is just a sequence. The key advantage for an Observable vs Swift's Sequence is that it can also receive elements asynchronously.
© Andy Jacobs, ixor.be 2016 14
![Page 15: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/15.jpg)
Event
Each value in the stream is defined as an event.• Next
• Error
• Completed
© Andy Jacobs, ixor.be 2016 15
![Page 16: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/16.jpg)
Interface
enum Event<Element> { case next(Element) // next element of a sequence case error(Swift.Error) // sequence failed with error case completed // sequence terminated successfully}
class Observable<Element> { func subscribe(_ observer: Observer<Element>) -> Disposable}
Sequences can have 0 or more elements.Once an error or completed event is received, the sequence cannot produce any other element.© Andy Jacobs, ixor.be 2016 16
![Page 17: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/17.jpg)
Disposing
A Disposable is used to disconnect the observable wrapper from its source, causing subscribed observer to stop receiving values from the underlying observable sequence.
error, completed, dispose(), takeUntil
all internal resources that compute sequence elements will be freed
© Andy Jacobs, ixor.be 2016 17
![Page 18: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/18.jpg)
SubjectsA Subject is a sort of bridge or proxy that is available in some implementations of Rx that acts as both an observer and Observable. Because it is an observer, it can subscribe to one or more Observables, and because it is an Observable, it can pass through the items it observes by reemitting them, and it can also emit new items.
© Andy Jacobs, ixor.be 2016 18
![Page 19: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/19.jpg)
Subjects
• PublishSubject
Broadcasts new events to all observers as of their time of the subscription.
• ReplaySubject
Broadcasts new events to all subscribers, and the specified bufferSize number of previous events to new subscribers.
• BehaviorSubject
Broadcasts new events to all subscribers, and the most recent (or initial) value to new subscribers.
• Variable
Wraps a BehaviorSubject, so it will emit the most recent (or initial) value to new subscribers. And Variable also maintains current value state. Variable will never emit an Error event. However, it will automatically emit a Completed event and terminate on deinit.
© Andy Jacobs, ixor.be 2016 19
![Page 20: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/20.jpg)
Hot ! vs Cold ❄
© Andy Jacobs, ixor.be 2016 20
![Page 21: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/21.jpg)
! vs ❄(demo)
© Andy Jacobs, ixor.be 2016 21
![Page 22: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/22.jpg)
SchedulersSchedulers abstract away the mechanism for performing work.
Different mechanisms for performing work include the current thread, dispatch queues, operation queues, new threads, thread pools, and run loops.
• CurrentThreadScheduler (default, serial)
• MainScheduler (serial)
• SerialDispatchQueueScheduler (serial)
• ConcurrentDispatchQueueScheduler (concurrent)
• OperationQueueScheduler (concurrent)
© Andy Jacobs, ixor.be 2016 22
![Page 23: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/23.jpg)
Real world examplelive coding
© Andy Jacobs, ixor.be 2016 23
![Page 24: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/24.jpg)
Tips
When you are using Rx, first try to compose built-in operators.
rxmarbles.com, rxmarbles.appstor.io
© Andy Jacobs, ixor.be 2016 24
![Page 25: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/25.jpg)
Tips
When you are using Rx, first try to compose built-in operators.
rxmarbles.com, rxmarbles.appstor.io
© Andy Jacobs, ixor.be 2016 25
![Page 26: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/26.jpg)
Tips
If using some (combination of) operators often, create your convenience operators.
extension ObservableType where E: MaybeCool {
@warn_unused_result(message="http://git.io/rxs.uo") public func coolElements() -> Observable<E> { return filter { e -> Bool in return e.isCool } }}
© Andy Jacobs, ixor.be 2016 26
![Page 27: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/27.jpg)
Tips
Avoid nesting subscribe calls at all cost
textField.rx_text.subscribe(onNext: { text in performURLRequest(text).subscribe(onNext: { result in ... }) .addDisposableTo(disposeBag)}).addDisposableTo(disposeBag)
© Andy Jacobs, ixor.be 2016 27
![Page 28: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/28.jpg)
Tips
Preferred way of chaining disposables by using operators.
textField.rx_text .flatMapLatest { text in // Assuming this doesn't fail and returns result on main scheduler, // otherwise `catchError` and `observeOn(MainScheduler.instance)` can be used to // correct this. return performURLRequest(text) } ... .addDisposableTo(disposeBag) // only one top most disposable
© Andy Jacobs, ixor.be 2016 28
![Page 29: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/29.jpg)
Credits..
• RxSwift documentation
• https://www.youtube.com/watch?v=7AqXBuJOJkY
• https://realm.io/news/nacho-soto-functional-reactive-programming/
• https://realm.io/news/altconf-ash-furrow-functional-reactive-swift/
© Andy Jacobs, ixor.be 2016 29
![Page 30: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/30.jpg)
rxdevcon.com
Saturday, October 7, 2017
© Andy Jacobs, ixor.be 2016 30
![Page 31: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/31.jpg)
Questions?
© Andy Jacobs, ixor.be 2016 31
![Page 32: FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new events to all observers as of their time of the subscription. • ReplaySubject](https://reader035.vdocuments.mx/reader035/viewer/2022070710/5ec67c88ae6d26098433807b/html5/thumbnails/32.jpg)
Thank you— @avalanched
© Andy Jacobs, ixor.be 2016 32