FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new...

32
FRP in Swift 3 101 © Andy Jacobs, ixor.be 2016 1

Transcript of FRP in Swift 3 - Meetupfiles.meetup.com/18561725/frp-swift.pdf · • PublishSubject Broadcasts new...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

! 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

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

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

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

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

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

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

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

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

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

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

Thank you— @avalanched

© Andy Jacobs, ixor.be 2016 32