Upgrade to Pro — share decks privately, control downloads, hide ads and more …

"Recative life" by Perto Korienev

"Recative life" by Perto Korienev

The world is reactive and we live now in an age of ReactiveCocoa5. Complexity, implicity, and visually strange declarative approach are the main blockers for adoption of reactive approaches for many developers. Petro Korienevтwill try to dig a bit into reactive and show few practices that might drop probable ReactiveCocoa downsides for you.

This talk was made for CocoaHeads Kyiv #11 which took place March 04 2017.

CocoaHeads Ukraine

March 09, 2017
Tweet

More Decks by CocoaHeads Ukraine

Other Decks in Programming

Transcript

  1. #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
  2. #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
  3. #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
  4. #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
  5. #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
  6. #cocoaheadsukraine Petro Korienev Reactive Life KILLER-FEATURES •Reactive KVO •UI &

    Foundation extensions •Rockstar macros & operations •Extensible architecture
  7. #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;
  8. #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]); }];
  9. #cocoaheadsukraine Petro Korienev Reactive Life EXTENSIBLE ARCHITECTURE •Example of filtering

    string https:/ /gist.github.com/soxjke/ b09175b252c597a2d468b9fbbe00cca3
  10. #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
  11. #cocoaheadsukraine Petro Korienev Reactive Life READABILITY - REACTIVEHELL •Example data

    structure to parse & solution: https:/ /gist.github.com/soxjke/ 4269e56942a0561f9adc60f9c7f3b41b
  12. #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
  13. #cocoaheadsukraine Petro Korienev Reactive Life REACTIVE SWIFT KEY DIFFS •Compile-time

    type-safety •Protocol-oriented design •Hot&cold observables
  14. #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() }
  15. #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
  16. #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
  17. #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 •…
  18. #cocoaheadsukraine Petro Korienev Reactive Life You may find these slides

    @ https:/ /speakerdeck.com/ soxjke/reactive-life THANKS!