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

Real World ReactiveCocoa ❤️

R. Peres
April 24, 2015

Real World ReactiveCocoa ❤️

R. Peres

April 24, 2015
Tweet

More Decks by R. Peres

Other Decks in Technology

Transcript

  1. Higher Order functions [[RACObserve(self, usernameTextField) filter:^(NSString *newName) { return [newName

    hasPrefix:@"j"]; }] subscribeNext:^(NSString *newName) { NSLog(@"%@", newName); }];
  2. Bind + Derivation RAC(self, loginButton.enabled) = [RACSignal combineLatest: combinedSignals reduce:^(NSString

    *p, NSString *u) { return @(p.length > 0 && u.length > 0); }]; NSArray *combinedSignals = @[RACObserve(self, passwordTextField), RACObserve(self, usernameTextField)];
  3. Composition [[[loginUserSignal flattenMap:^(User *user) { // Return a signal that

    loads cached messages for the user. return [client loadCachedMessagesForUser:user]; }] flattenMap:^(NSArray *messages) { // Return a signal that fetches any remaining messages. return [client fetchMessagesAfterMessage:messages.lastObject]; }] subscribeNext:^(NSArray *newMessages) { NSLog(@"New messages: %@", newMessages); } completed:^{ NSLog(@"Fetched all messages."); }];
  4. “The power of these abstractions are very hard to convey

    to someone who has never had the privilege of learning them” Bartosz Milewski
  5. @[OCTNotification1, OCTNotification2, OCTNotification3…]; Map from an Array to a NSOrderedSet

    fold @[“RuiPeres/Octify, iOS-Goodies/iOS-Goodies, ReactiveCocoa/ReactiveCocoa];
  6. @“ReactiveCocoa/ReactiveCocoa” @“RuiAAPeres/Octify” @“iOS-Goodies/iOS-Goodies” [[signal map:^id(NSArray *notifications) { }]; @property(nonatomic,strong)NSOrderedSet *sections;

    return [NSOrderedSet orderedSetWithArray: ]; RAC(self,sections) = [notifications.rac_sequence foldLeftWithStart:[NSMutableOrderedSet orderedSet] reduce:^id(NSMutableOrderedSet *accumulator, OCTResponse *response) { OCTNotification *notification = response.parsedResult; [accumulator addObject:[notification ownerAndLogin]]; return accumulator; }].array
  7. + (RACSignal *)storedNotifications { return [RACSignal createSignal: ^RACDisposable *(id<RACSubscriber> subscriber)

    { NSError *error = // if something fails NSArray *notifications = // get notifications if (error) { [subscriber sendError:[NSError storedError]]; } else { [subscriber sendNext:notifications]; [subscriber sendCompleted]; } return nil; }]; }
  8. RACSequence *handlers = @[ @"twitter://user?screen_name=%@", @"tweetbot://%@/user_profile/%@", @"twitterrific:///profile?screen_name=%@", @“https://twitter.com/%@"].rac_sequence; return [[[[handlers

    map:^id(NSString *value) { return [NSString stringWithFormat:value,screenName,screenName]; }] map:^id(NSString *value) { return [NSURL URLWithString:value]; }] filter:^BOOL(NSURL *value) { return [[UIApplication sharedApplication] canOpenURL:value]; }] head];