Slide 1

Slide 1 text

ReactiveCocoa Michael James Friday, June 14, 13

Slide 2

Slide 2 text

What is ReactiveCocoa (RAC)? • An Obj-C framework for Functional Reactive Programming • Provides a simple way to process a stream of values • Created by GitHub’s Josh Abernathy • Inspired by .Net’s Reactive Extensions (Rx) Friday, June 14, 13

Slide 3

Slide 3 text

Common Uses for ReactiveCocoa • Cleaner KVO alternative • Updating UI based on current state of data • Great way to setup how to process values that you haven’t received yet • like network callbacks Friday, June 14, 13

Slide 4

Slide 4 text

ReactiveCocoa’s Core Classes Friday, June 14, 13

Slide 5

Slide 5 text

RACStream • An abstract representation of a stream of values • Values can only be retrieved sequentially • Not like an NSArray • Values can be available immediately or in the future Friday, June 14, 13

Slide 6

Slide 6 text

RACStream Operations - (instancetype)map:(id (^)(id value))block • transforms the values in the stream to another object - (instancetype)filter:(BOOL (^)(id value))block • only sends values that pass the test block • Many more defined in RACStream.h Friday, June 14, 13

Slide 7

Slide 7 text

RACSequence • An immutable RACStream that acts like a collection • is lazily evaluated • can only access values sequentially • cannot contain nil • RAC adds categories on Cocoa’s collection classes that create sequences from them Friday, June 14, 13

Slide 8

Slide 8 text

RACSignal • A RACStream that can be subscribed to. • Subscribers receive 3 kinds of events 1. Next - The next value in the stream 2. Error - An error occurred before the signal finished providing values 3. Completed - The signal is done providing values Friday, June 14, 13

Slide 9

Slide 9 text

RACSignal Operations + (RACSignal *)combineLatest: (id)signals reduce: (id)reduceBlock • returns a signal that produces values that result from taking the latest values from each signal in signals argument and performing some logic on them to produce a single value (performed by reduceBlock) • Many more defined in RACSignal+Operations.h Friday, June 14, 13

Slide 10

Slide 10 text

RACSubject • A RACSignal that can be manually controlled • You control what and when to send the next, error, and completed events to its subscribers • Often used to bridge non-RAC code to RAC’s signals-based code Friday, June 14, 13

Slide 11

Slide 11 text

RACCommand • A RACSignal that is triggered in response to an action (think UI target/action) • ReactiveCocoa provides categories on core AppKit and UIKit classes to allow processing their target/actions via RACSignals and RACCommands Friday, June 14, 13

Slide 12

Slide 12 text

RAC macros and Utilities RACAble(object, keypath) • Returns a signal that observes the object’s keypath for changes • Alternative way to do KVO RAC(object, keypath) = signal • Keeps the object’s keypath updated with values sent to the signal Friday, June 14, 13

Slide 13

Slide 13 text

RAC macros and Utilities (cont’d) @weakify(object)/ @strongify(object) • Convenience macros to work around retain cycles when dealing with blocks • Defined in ExtScope.h (in libextobjc dependency) • Must be explicitly imported Friday, June 14, 13

Slide 14

Slide 14 text

Demo Friday, June 14, 13

Slide 15

Slide 15 text

Links GitHub Project https://github.com/ReactiveCocoa/ReactiveCocoa Demo Code https://github.com/umjames/RCPlayground/tree/feature/feedzilla_search Framework Overview https://github.com/ReactiveCocoa/ReactiveCocoa/blob/v1.9.3/Documentation/ FrameworkOverview.md Blog Articles https://github.com/blog/1107-reactivecocoa-for-a-better-world http://nshipster.com/reactivecocoa/ http://cocoasamurai.blogspot.com/2013/03/basic-mvvm-with-reactivecocoa.html Friday, June 14, 13

Slide 16

Slide 16 text

Questions? Friday, June 14, 13