"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.

Db84cf61fdada06b63f43f310b68b462?s=128

CocoaHeads Ukraine

March 09, 2017
Tweet

Transcript

  1. REACTIVE LIFE Petro Korienev iOS Team Lead @ Sigma Software

  2. #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
  3. #cocoaheadsukraine Petro Korienev Reactive Life THIS IS MARS HE’S SCEPTIC

    ABOUT REACTIVECOCOA
  4. #cocoaheadsukraine Petro Korienev Reactive Life CONTENTS •What’s RAC •RAC Objective-C

    •RAC 5 •Examples
  5. #cocoaheadsukraine Petro Korienev Reactive Life “What’s RAC?”

  6. #cocoaheadsukraine Petro Korienev Reactive Life

  7. #cocoaheadsukraine Petro Korienev Reactive Life Latest ObjC release is 2.5

  8. #cocoaheadsukraine Petro Korienev Reactive Life Latest Swift is 5.0.1

  9. #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
  10. #cocoaheadsukraine Petro Korienev Reactive Life OBJECTIVE-C: ReactiveCocoa 2.5 highlights

  11. #cocoaheadsukraine Petro Korienev Reactive Life Rough class diagram

  12. #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
  13. #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
  14. #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
  15. #cocoaheadsukraine Petro Korienev Reactive Life And now, why it’s ReactiveCocoa

    ?
  16. #cocoaheadsukraine Petro Korienev Reactive Life KILLER-FEATURES •Reactive KVO •UI &

    Foundation extensions •Rockstar macros & operations •Extensible architecture
  17. #cocoaheadsukraine Petro Korienev Reactive Life NOW MARS GONNA TRY IT

  18. #cocoaheadsukraine Petro Korienev Reactive Life REACTIVE KVO - (RACSignal *)rac_valuesForKeyPath:(NSString

    *)keyPath observer:(__weak NSObject *)observer;
  19. #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;
  20. #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]); }];
  21. #cocoaheadsukraine Petro Korienev Reactive Life @weakify(self);
 [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIApplicationWillResignActive Notification

    object:nil] subscribeNext:^(NSNotification *note) {
 @strongify(self); … }]; ROCKSTAR MACROS & OPERATIONS
  22. #cocoaheadsukraine Petro Korienev Reactive Life EXTENSIBLE ARCHITECTURE •Example of filtering

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

    structure to parse & solution: https:/ /gist.github.com/soxjke/ 4269e56942a0561f9adc60f9c7f3b41b
  25. #cocoaheadsukraine Petro Korienev Reactive Life

  26. #cocoaheadsukraine Petro Korienev Reactive Life MARS DOESN’T LIKE REACTIVECOCOA STACKTRACE

  27. #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
  28. #cocoaheadsukraine Petro Korienev Reactive Life WHAT ABOUT SWIFT?

  29. #cocoaheadsukraine Petro Korienev Reactive Life SWIFT ReactiveCocoa 5

  30. #cocoaheadsukraine Petro Korienev Reactive Life REACTIVE SWIFT KEY DIFFS •Compile-time

    type-safety •Protocol-oriented design •Hot&cold observables
  31. #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() }
  32. #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
  33. #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
  34. #cocoaheadsukraine Petro Korienev Reactive Life MORE USEFUL CLASSES •MutableProperty, DynamicProperty

    •UIScheduler •Action •…
  35. #cocoaheadsukraine Petro Korienev Reactive Life MARS IS TIRED

  36. #cocoaheadsukraine Petro Korienev Reactive Life EXAMPLE 1. PLACES SEARCH •https:/

    /github.com/soxjke/ReactiveLife
  37. #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 •…
  38. #cocoaheadsukraine Petro Korienev Reactive Life You may find these slides

    @ https:/ /speakerdeck.com/ soxjke/reactive-life THANKS!
  39. #cocoaheadsukraine Petro Korienev Reactive Life ME: https://twitter.com/soxjke https://github.com/soxjke https://www.facebook.com/soxjke https://stackoverflow.com/users/2392973