Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Let's be functional-reactive in Cocoa world
Search
Igor Vasilenko
July 17, 2016
Programming
0
120
Let's be functional-reactive in Cocoa world
Slides from talk on MO Conf 2016
mo.dev.by
Igor Vasilenko
July 17, 2016
Tweet
Share
More Decks by Igor Vasilenko
See All by Igor Vasilenko
ReactiveCocoa. As all silent
spbvasilenko
0
48
Other Decks in Programming
See All in Programming
ADRを一年運用してみた/adr_after_a_year
hanhan1978
7
2.4k
Node.js v22 で変わること
yosuke_furukawa
PRO
11
3.9k
Go製Webアプリケーションのエラーとの向き合い方大全、あるいはやっぱりスタックトレース欲しいやん / Kyoto.go #50
utgwkk
6
1.7k
GraphQLサーバの構成要素を整理する #ハッカー鮨 #tsukijigraphql / graphql server technology selection
izumin5210
4
890
Goのエラースタックトレースの歴史と今後
sonatard
9
1.7k
Fragment Composition of GraphQL
quramy
13
1.4k
AppRouter Panel Talk
yosuke_furukawa
PRO
1
430
Exploring the Implementation of “t.Run”, “t.Parallel”, and “t.Cleanup”
akarin
1
100
MicrosoftのPlatform Engineeringガイドを読んで実際になにかやってみた
ymd65536
1
490
"config" ってなんだ? / What is "config"?
okashoi
0
250
Hanami and htmx
bkuhlmann
0
220
Scalable Customer Journey Orchestration (CJO)
lewuathe
0
410
Featured
See All Featured
Infographics Made Easy
chrislema
238
18k
Atom: Resistance is Futile
akmur
260
25k
Building Better People: How to give real-time feedback that sticks.
wjessup
356
18k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
242
1.2M
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
14
1.5k
A Modern Web Designer's Workflow
chriscoyier
689
190k
The Language of Interfaces
destraynor
151
23k
Testing 201, or: Great Expectations
jmmastey
29
6.4k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
How GitHub (no longer) Works
holman
305
140k
The MySQL Ecosystem @ GitHub 2015
samlambert
244
12k
Music & Morning Musume
bryan
41
5.6k
Transcript
Let’s be functional-reactive in Cocoa world
About me iOS developer at YOTA Official member of Typhoon
framework vasilenkoigor i_vasilenko igorvasilenko.me
Agenda FRP explained ReactiveCocoa explained Practice with RAC RAC vs.
RxSwift Conclusion
Agenda FRP explained ReactiveCocoa explained Practice with RAC RAC vs.
RxSwift Conclusion
FRP explained
FRP explained «Is a programming paradigm for reactive programming using
the building blocks of functional programming.» - Wikipedia
None
FRP explained #BULLSHIT
FRP explained • Reactive programming is programming with asynchronous data
streams • Amazing toolbox of functions to combine, create and filter any of those streams Let’s cut the bullshit
x: 10 x: 20 x: 30 x: 40 x: 50
Data stream
FRP explained • Reactive programming is programming with asynchronous data
streams • Amazing toolbox of functions to combine, create and filter any of those streams Let’s cut the bullshit
FRP explained Why should you consider adopting RP? • Code
will likely be more concise • Efficiency • Architecture • Hot-swapping • Binding
Agenda FRP explained ReactiveCocoa explained Practice with RAC RAC vs.
RxSwift Conclusion
Agenda FRP explained ReactiveCocoa explained Practice with RAC RAC vs.
RxSwift Conclusion
ReactiveCocoa explained
ReactiveCocoa explained • ReactiveCocoa (RAC) is a framework developed by
GitHub • ReactiveCocoa is an implementation FRP on iOS/macOS • Currently framework version - 4.2.1
ReactiveCocoa explained Signals Signal is an object that can emit
Events.
ReactiveCocoa explained Signals public final class Signal<Value, Error: ErrorType>
ReactiveCocoa explained Signals let mySignal = Signal<Int, NSError>({ observer in
observer.sendNext(1) observer.sendNext(2) /* send other events to observer */ return nil })
ReactiveCocoa explained Events There are four types of events that
a signal can emit
ReactiveCocoa explained Events Next(Value) events are most common ones. They
carry a certain value;
ReactiveCocoa explained Events Completed event signals the successful completion of
a signal;
ReactiveCocoa explained Events Failed(Error) event carries an error object that
reflects the reason of failed signal completion;
ReactiveCocoa explained Events Interrupted is a termination event that signifies
neither a successful, nor failed completion of a signal.
ReactiveCocoa explained Timeline of Events /^Next*(Failed|Completed|Interrupted)?$/
ReactiveCocoa explained Observers You can think of observers as functions
that do something with event when it arrives.
ReactiveCocoa explained Observers • observeNext: observe only Next events carrying
a value • observeFailed: observe only Failed event that carry an error object • observeCompleted: observe only signal completion event
ReactiveCocoa explained Pipes In the days of RAC 2.x this
used to be represented by RACSubject class instances. let (signal, observer) = Signal<Int, NSError>.pipe()
ReactiveCocoa explained Pipes signal.observeNext({ print($0)}) observer.sendNext(0) // prints "0" observer.sendNext(1)
// prints "1"
ReactiveCocoa explained Operators map: changes function from one type to
another or modifies the value retaining the type someSignal.map { (intValue) -> String in return "value: \(intValue)" }
ReactiveCocoa explained Operators filter: forwards only the values that pass
a given predicate closure; someSignal.filter { (intValue) -> Bool in return intValue > 0 }
ReactiveCocoa explained Operators take: takes up to N values sent
by the signal someSignal.take(1).observeNext { (intValue) in // Next event will be once received }
ReactiveCocoa explained Operators throttle: sends new signal’s values no more
often than provided time interval someSignal.throttle(30,onScheduler:UIScheduler())
ReactiveCocoa explained Operators let combineSignals = combineLatest(numbersSignal,lettersSignal) .on { (intValue,
stringValue) in print(intValue) print(stringValue) } combineLatest: a free function that combines values from variable number of signals
ReactiveCocoa explained Operators combineSignals.reduce("", { (startValue, valuesTuple) in return valuesTuple.1
}).observeNext { (stringValue) in print(stringValue) } reduce: upon signal completion combines all sent values using provided function
ReactiveCocoa explained SignalProducers You’d want a pull-driven stream of events
so that you could receive events on demand.
ReactiveCocoa explained SignalProducers SignalProducer <String, NSError> {(observer, disposable) in ///
some task (api request, saving to database, etc.) }
ReactiveCocoa explained SignalProducers Another big difference of the SignalProducer is
that it can contain side-effects.
ReactiveCocoa explained SignalProducers producer.on(started: { /// Some work }, event:
{ (event) in /// Some work }) other side-effects: failed, interrupted, completed, terminated, disposed
ReactiveCocoa explained MutableProperty Mutable property is a concrete implementation of
read-write property and is probably mostly used type among other ones. var bankAccountBalance = MutableProperty<Double>(100)
ReactiveCocoa explained MutableProperty bankAccountBalance.value = 200; bankAccountBalance.signal.observeNext { (value) in
/// Some work } Observe and change value
ReactiveCocoa explained Schedulers When receiving a signal or producer from
unknown code, it can be difficult to know which thread events will arrive upon.
ReactiveCocoa explained Schedulers someSignal.observeOn(UIScheduler()).observeNext { (value) in /// observe on
Main Thread } Whenever such a guarantee is important, the observeOn operator should be used to force events to be received upon a specific scheduler.
ReactiveCocoa explained Schedulers Schedulers are similar to Grand Central Dispatch
queues, but schedulers support cancellation (via disposables), and always execute serially.
Agenda FRP explained ReactiveCocoa explained Practice with RAC RAC vs.
RxSwift Conclusion
Agenda FRP explained ReactiveCocoa explained Practice with RAC RAC vs.
RxSwift Conclusion
Practice with RAC github.com/vasilenkoigor/MOConf2016Practice
Agenda FRP explained ReactiveCocoa explained Practice with RAC RAC vs.
RxSwift Conclusion
Agenda FRP explained ReactiveCocoa explained Practice with RAC RAC vs.
RxSwift Conclusion
RAC vs. RxSwift
RAC vs. RxSwift • ReactiveX - a group which created
a common API for FRP implementations • RxSwift is a relatively recent addition to ReactiveX • Currently framework version - 2.6.0
RAC vs. RxSwift Hot and cold RxSwift doesn’t support differentiate
between hot and cold signals Observable <String>.just("Say hello RxSwift")
RAC vs. RxSwift Error handling RxSwift doesn’t support parameterized type
for the error case Observable <T>
RAC vs. RxSwift UI bindings • RxSwift bindings are a
joy to use • ReactiveCocoa bindings only with custom extensions
RAC vs. RxSwift RAC in big projects? • WhatsApp •
Github • Yota • Yandex • SoundCloud
RAC vs. RxSwift Why YOTA using ReactiveCocoa? • We want
to be able to better describe our system • We want having different types to differentiate between hot and cold signals • We want a battle tested framework, used by many people, in many projects
Agenda FRP explained ReactiveCocoa explained Practice with RAC RAC vs.
RxSwift Conclusion
Agenda FRP explained ReactiveCocoa explained Practice with RAC RAC vs.
RxSwift Conclusion
Thank you! <3