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

ReactiveCocoa at MobiDevDay 2013

ReactiveCocoa at MobiDevDay 2013

https://github.com/andrewsardone/RACMobiDevDay
Recording: https://vimeo.com/65637501

These are the slides from my MobiDevDay 2013 talk on ReactiveCocoa.

ReactiveCocoa (RAC) is an Objective-C framework for Functional Reactive Programming. It builds off of Cocoa's own key-value observing mechanism to provide a high-level, declarative API for composing operations on streams of data, state, and asynchronous tasks. What does all of that mean? We'll find out as we explore RAC's potential for helping us better reason about the creation of our apps.

Also included in the Declarative Programming section is https://github.com/mneorr/ObjectiveSugar

Additional resources:

https://github.com/ReactiveCocoa
https://pinboard.in/u:andrewsardone/t:ReactiveCocoa
https://github.com/andrewsardone/RACMobiDevDay
http://nsbrief.com/81-justin-spahr-summers/
http://j.mp/joshaberio
http://j.mp/reactiveextensions

Andrew Sardone

May 04, 2013
Tweet

More Decks by Andrew Sardone

Other Decks in Programming

Transcript

  1. Objective-C OOP #import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomic,

    copy) NSString *name; - (void)say:(NSString *)message withExclaim:(BOOL)exclaim; @end @implementation Person - (void)say:(NSString *)message withExclaim:(BOOL)exclaim { if (exclaim) { message = [message uppercaseString]; } NSLog(@"%@", message); } @end
  2. NSArray *array = @[@"foo", @"bar", @"baz"]; NSMutableArray *uppercaseArray = [NSMutableArray

    array]; for (int i = 0; i < array.count; i++ ) { uppercaseArray[i] = [array[i] uppercaseString]; } // uppercaseArray: @[ @"FOO", @"BAR", @"BAZ" ] id u = [@[@"foo", @"bar", @"baz"] map:^id(NSString *s) { return [s uppercaseString]; }]; // u: @[ @"FOO", @"BAR", @"BAZ" ] Describes how to compute Describes what to compute
  3. Taps Keyboard events GPS events Web service responses … UI

    updates File on disk New DB record Web service request … INPUT w!r" I#p$% &#' O$%p$% by Josh Abernathy http://j.mp/joshaberio OUTPUT
  4. a = 2 b = 2 c = a +

    b # c is 4 b = 3 # now what’s the value of c?
  5. a := 2 # signal b := 2 # signal

    c := a + b # signal binding # other signals b 2 3 c a + b 4 5 2 a 4 7 5 6 1
  6. KVO property UI events Network request Async work … Values

    over time https://speakerdeck.com/joshaber/reactivecocoa-for-a-better-world
  7. @interface RACSignal : RACStream [RACAble(self.username) subscribeNext:^(NSString *newName) { NSLog(@"%@", newName);

    }]; https://github.com/ReactiveCocoa/ReactiveCocoa/blob/master/README.md next completed error sending a single value done sending all values & successful sending values but terminated in error
  8. [[self.textField.rac_textSignal map:^id(id text) { return [text stringByAppendingString:@" – map'd it"];

    }] subscribeNext:^(NSString *text) { NSLog(@"Incoming text signal: %@", text); }];
  9. RACSignal *url = [[[[button rac_signalForControlEvents:UIControlEventTouchUpInside] map:^id(id _) { return NSString

    *s = [@[ [NSURL URLWithString:@"http://nutshell.com"], [NSURL URLWithString:@"http://google.com"], [NSURL URLWithString:@"http://wikipedia.org"], [NSURL URLWithString:@"http://apple.com"], [NSURL URLWithString:@"http://github.com/ReactiveCocoa/ReactiveCocoa"], ] aps_randomObject]; }] publish] autoconnect]; RACSignal *html = [[url flattenMap:^RACStream *(NSURL *url) { NSStringEncoding encoding = NSUTF8StringEncoding; return [NSString rac_readContentsOfURL:url usedEncoding:&encoding scheduler:[RACScheduler scheduler]; }] deliverOn:RACScheduler.mainThreadScheduler]; RAC(label, text) = [url map:^id(NSURL *u) { return u.absoluteString; }]; [webView rac_liftSelector:@selector(loadHTMLString:baseURL:) withObjects:html, url];
  10. field.enabled = NO; [worker doWorkWithCompletionBlock:^(id result, NSError *error) { field.enabled

    = YES; if (error) { /* handle error */ } else /* handle result */ }]; RACCommand *doWork = [worker doWorkAsCommand]; RAC(field, enabled) = [RACAble(doWork, executing) not]; // later, when we want to do the work // our UI pipes are plugged together so we don’t need to // worry about that [doWork execute:sender]; // handle result of doWork somewhere else where it matters … less imperative … more declarative
  11. Andrew Sardone twitter: @andrewa2 app.net: @andrewsardone github.com/andrewsardone github.com/ReactiveCocoa pinboard.in/u:andrewsardone/t:ReactiveCocoa github.com/andrewsardone/RACMobiDevDay

    nsbrief.com/81-justin-spahr-summers/ Input and Output – j.mp/joshaberio Reactive Extensions (Rx) – j.mp/reactiveextensions