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
55
Other Decks in Programming
See All in Programming
讓數據說話:用 Python、Prometheus 和 Grafana 講故事
eddie
0
390
みんなでプロポーザルを書いてみた
yuriko1211
0
210
Kaigi on Rails 2024 - Rails APIモードのためのシンプルで効果的なCSRF対策 / kaigionrails-2024-csrf
corocn
5
3.8k
Quine, Polyglot, 良いコード
qnighy
4
630
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
590
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
170
Dev ContainersとGitHub Codespacesの素敵な関係
ymd65536
1
140
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
110
リリース8年目のサービスの1800個のERBファイルをViewComponentに移行した方法とその結果
katty0324
5
4.2k
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
510
macOS でできる リアルタイム動画像処理
biacco42
9
2.3k
Better Code Design in PHP
afilina
PRO
0
110
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Writing Fast Ruby
sferik
627
61k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
[RailsConf 2023] Rails as a piece of cake
palkan
51
4.9k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
820
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Bash Introduction
62gerente
608
210k
Designing for humans not robots
tammielis
249
25k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
KATA
mclloyd
29
14k
Code Review Best Practice
trishagee
64
17k
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