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
59
Other Decks in Programming
See All in Programming
Lottieアニメーションをカスタマイズしてみた
tahia910
0
120
Conform を推す - Advocating for Conform
mizoguchicoji
3
690
Writing documentation can be fun with plugin system
okuramasafumi
0
120
ペアーズでの、Langfuseを中心とした評価ドリブンなリリースサイクルのご紹介
fukubaka0825
2
310
Amazon Bedrock Multi Agentsを試してきた
tm2
1
280
Honoのおもしろいミドルウェアをみてみよう
yusukebe
1
200
CI改善もDatadogとともに
taumu
0
110
TokyoR116_BeginnersSession1_環境構築
kotatyamtema
0
110
苦しいTiDBへの移行を乗り越えて快適な運用を目指す
leveragestech
0
340
Honoをフロントエンドで使う 3つのやり方
yusukebe
7
3.1k
プログラミング言語学習のススメ / why-do-i-learn-programming-language
yashi8484
0
130
GitHub Actions × RAGでコードレビューの検証の結果
sho_000
0
250
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
A Tale of Four Properties
chriscoyier
158
23k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
31
2.1k
Building Your Own Lightsaber
phodgson
104
6.2k
Designing for humans not robots
tammielis
250
25k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
410
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
The Cult of Friendly URLs
andyhume
78
6.2k
Music & Morning Musume
bryan
46
6.3k
The Invisible Side of Design
smashingmag
299
50k
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