Share
Embed
Transcript
16 slides
-
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 -
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 -
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 -
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 -
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 -
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 -
RACSignal Operations + (RACSignal *)combineLatest: (id<NSFastEnumeration>)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 -
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 -
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 -
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 -
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 -
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