Share
Embed
Reactive Life
Petro Korienev
March 04, 2017
Transcript
39 slides
-
#cocoaheadsukraine Petro Korienev Reactive Life DISCLAIMER Many of you may
be sceptic about ReactiveCocoa, so my goal for today will be to show that it’s not that bad. I don’t pretend to be expert in functional, reactive or functional reactive programming, but I’d like to show you how it can solve problems. At least for me -
#cocoaheadsukraine Petro Korienev Reactive Life THIS IS MARS HE’S SCEPTIC
ABOUT REACTIVECOCOA -
#cocoaheadsukraine Petro Korienev Reactive Life THE MOST IMPORTANT DEFINITION IS
SIGNAL •Sends value/(s) to its observers •Sends completions and errors •Can participate in expressions, resulting in new signals or producing state •Signal is universal abstraction -
#cocoaheadsukraine Petro Korienev Reactive Life RACSTREAM •Abstract class representing any
stream of values •RACStream is a monad •RACStream’s can be combined into arbitrary functional expressions -
#cocoaheadsukraine Petro Korienev Reactive Life RACSIGNAL: RACSTREAM •RACSignal is a
derivate of RACStream that can be observable with side-effects •RACSignal is the entity that manages the concept of subscription •Subscription has it’s start and finish •There can be multiple subscriptions -
#cocoaheadsukraine Petro Korienev Reactive Life RACSEQUENCE: RACSTREAM •RACSequence is a
derivate of RACStream that converts composite type to stream •Thus RACSequence makes array, dictionary, string a monad •Thus functional additions are added to Foundation -
#cocoaheadsukraine Petro Korienev Reactive Life KILLER-FEATURES •Reactive KVO •UI &
Foundation extensions •Rockstar macros & operations •Extensible architecture -
#cocoaheadsukraine Petro Korienev Reactive Life REACTIVE KVO - (RACSignal *)rac_valuesForKeyPath:(NSString
*)keyPath observer:(__weak NSObject *)observer; -
#cocoaheadsukraine Petro Korienev Reactive Life UI & FOUNDATION EXTENSIONS -
(RACSignal *)rac_signalForControlEvents: (UIControlEvents)controlEvents; - (RACSignal *)rac_signalForSelector:(SEL)selector fromProtocol:(Protocol *)protocol; - (RACSignal *)rac_addObserverForName:(NSString *)notificationName object:(id)object; -
#cocoaheadsukraine Petro Korienev Reactive Life ROCKSTAR MACROS & OPERATIONS RAC(self,
regBtn.enabled) = [RACSignal combineLatest: @[RACObserve(self, passwordField.text), RACObserve(self, passwordConfirmation.text)] reduce:^(NSString *password, NSString *passwordConfirmation) { return @([passwordConfirmation isEqualToString:password]); }]; -
#cocoaheadsukraine Petro Korienev Reactive Life @weakify(self); [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIApplicationWillResignActive Notification
object:nil] subscribeNext:^(NSNotification *note) { @strongify(self); … }]; ROCKSTAR MACROS & OPERATIONS -
#cocoaheadsukraine Petro Korienev Reactive Life EXTENSIBLE ARCHITECTURE •Example of filtering
string https:/ /gist.github.com/soxjke/ b09175b252c597a2d468b9fbbe00cca3 -
#cocoaheadsukraine Petro Korienev Reactive Life OBJECTIVE-C DOWNSIDES •Type-unsafe subscriptions. •Easy-to-write
retain cycles •Debugging complexity •Values-over-time are difficult to test •Complex expressions are often way unreadable. Especially without ReactiveCocoa experience -
#cocoaheadsukraine Petro Korienev Reactive Life READABILITY - REACTIVEHELL •Example data
structure to parse & solution: https:/ /gist.github.com/soxjke/ 4269e56942a0561f9adc60f9c7f3b41b -
#cocoaheadsukraine Petro Korienev Reactive Life FEW DEBUGGING TIPS •use rac_willDeallocSignal
to observe lifetimes when debugging intermittent crashes •use RACSignal (Debug) category with log methods •try breaking complex signal expressions and inject side effects on each step -
#cocoaheadsukraine Petro Korienev Reactive Life REACTIVE SWIFT KEY DIFFS •Compile-time
type-safety •Protocol-oriented design •Hot&cold observables -
#cocoaheadsukraine Petro Korienev Reactive Life COMPILE-TIME TYPE-SAFETY public final class
Signal<Value, Error: Swift.Error> public struct SignalProducer<Value, Error: Swift.Error> public protocol ObserverProtocol { associatedtype Value associatedtype Error: Swift.Error func send(value: Value) func send(error: Error) func sendCompleted() func sendInterrupted() } -
#cocoaheadsukraine Petro Korienev Reactive Life SIGNAL •“Hot” observable, subscription shouldn’t
involve side-effects according to guidelines •Observing results in Disposable? •There’re “value”, “error”, “completed”, “interrupted” events -
#cocoaheadsukraine Petro Korienev Reactive Life SIGNALPRODUCER •“Cold” observable, starting it
may produce a signal, indicating status of some underlying operation. •Observing results in Disposable, used to terminate observation & stop sending values -
#cocoaheadsukraine Petro Korienev Reactive Life EXAMPLE 1. PLACES SEARCH •https:/
/github.com/soxjke/ReactiveLife -
#cocoaheadsukraine Petro Korienev Reactive Life WHAT’S STILL UNCOVERED •In-depth look
into operators •Action •Building framework for tests •Building your own reactive UI framework •UIScheduler •Action •… -
#cocoaheadsukraine Petro Korienev Reactive Life You may find these slides
@ https:/ /speakerdeck.com/ soxjke/reactive-life THANKS!