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
64
Other Decks in Programming
See All in Programming
AIプログラマーDevinは PHPerの夢を見るか?
shinyasaita
1
210
PHP 8.4の新機能「プロパティフック」から学ぶオブジェクト指向設計とリスコフの置換原則
kentaroutakeda
2
760
Azure AI Foundryではじめてのマルチエージェントワークフロー
seosoft
0
160
MDN Web Docs に日本語翻訳でコントリビュートしたくなる
ohmori_yusuke
1
120
0626 Findy Product Manager LT Night_高田スライド_speaker deck用
mana_takada
0
160
効率的な開発手段として VRTを活用する
ishkawa
0
130
チームのテスト力を総合的に鍛えて品質、スピード、レジリエンスを共立させる/Testing approach that improves quality, speed, and resilience
goyoki
4
760
RailsGirls IZUMO スポンサーLT
16bitidol
0
180
レベル1の開発生産性向上に取り組む − 日々の作業の効率化・自動化を通じた改善活動
kesoji
0
140
PostgreSQLのRow Level SecurityをPHPのORMで扱う Eloquent vs Doctrine #phpcon #track2
77web
2
510
Goで作る、開発・CI環境
sin392
0
230
AIと”コードの評価関数”を共有する / Share the "code evaluation function" with AI
euglena1215
1
150
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
37
3.3k
Automating Front-end Workflow
addyosmani
1370
200k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
20k
Code Reviewing Like a Champion
maltzj
524
40k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
680
The Art of Programming - Codeland 2020
erikaheidi
54
13k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Writing Fast Ruby
sferik
628
62k
Designing for humans not robots
tammielis
253
25k
Optimizing for Happiness
mojombo
379
70k
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