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

Let's be functional-reactive in Cocoa world

Let's be functional-reactive in Cocoa world

Slides from talk on MO Conf 2016
mo.dev.by

Igor Vasilenko

July 17, 2016
Tweet

More Decks by Igor Vasilenko

Other Decks in Programming

Transcript

  1. About me iOS developer at YOTA Official member of Typhoon

    framework vasilenkoigor i_vasilenko igorvasilenko.me
  2. FRP explained «Is a programming paradigm for reactive programming using

    the building blocks of functional programming.» - Wikipedia
  3. 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
  4. 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
  5. FRP explained Why should you consider adopting RP? • Code

    will likely be more concise • Efficiency • Architecture • Hot-swapping • Binding
  6. ReactiveCocoa explained • ReactiveCocoa (RAC) is a framework developed by

    GitHub • ReactiveCocoa is an implementation FRP on iOS/macOS • Currently framework version - 4.2.1
  7. ReactiveCocoa explained Signals let mySignal = Signal<Int, NSError>({ observer in

    observer.sendNext(1) observer.sendNext(2) /* send other events to observer */ return nil })
  8. ReactiveCocoa explained Events Interrupted is a termination event that signifies

    neither a successful, nor failed completion of a signal.
  9. 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
  10. 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()
  11. 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)" }
  12. ReactiveCocoa explained Operators filter: forwards only the values that pass

    a given predicate closure;
 someSignal.filter { (intValue) -> Bool in return intValue > 0 }
  13. 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 }
  14. ReactiveCocoa explained Operators throttle: sends new signal’s values no more

    often than provided time interval someSignal.throttle(30,onScheduler:UIScheduler())
  15. 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
  16. 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

  17. ReactiveCocoa explained SignalProducers producer.on(started: { /// Some work }, event:

    { (event) in /// Some work }) other side-effects: failed, interrupted, completed, terminated, disposed

  18. 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)
  19. ReactiveCocoa explained Schedulers When receiving a signal or producer from

    unknown code, it can be difficult to know which thread events will arrive upon.
  20. 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.
  21. ReactiveCocoa explained Schedulers Schedulers are similar to Grand Central Dispatch

    queues, but schedulers support cancellation (via disposables), and always execute serially.
  22. 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
  23. RAC vs. RxSwift Hot and cold RxSwift doesn’t support differentiate

    between hot and cold signals Observable <String>.just("Say hello RxSwift")
  24. RAC vs. RxSwift UI bindings • RxSwift bindings are a

    joy to use • ReactiveCocoa bindings only with custom extensions
  25. RAC vs. RxSwift RAC in big projects? • WhatsApp •

    Github • Yota • Yandex • SoundCloud
  26. 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