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

Introduction to ReactiveCocoa and FRP by Alexan...

Introduction to ReactiveCocoa and FRP by Alexander Zaporozhchenko

Introduction to ReactiveCocoa and FRP by Alexander Zaporozhchenko

GDG Ternopil

October 20, 2015
Tweet

More Decks by GDG Ternopil

Other Decks in Programming

Transcript

  1. Что такое FRP? Реактивное программирование: • потоки данных • распространение

    данных • реакция на events Функциональный подход: • набор функций • отсутствие состояний и мутабельности • зависимость только от входных данных FRP = (Reactive + Functional) Programming;
  2. Imperative Reactive x = 2; y = 3; z =

    x + y; // z = 5 x = 1; // z = 5 y = -10; // z = 5 x = 2; y = 3; z = x + y; // z = 5 x = 1; // z = 3 y = -10; // z = -8
  3. NSMutableArray *returnArray = [NSMutableArray new]; for (NSString *string in array)

    { if ([string hasPrefix:@"RAC"]) { [returnArray addObject:[string stringByReplacingOccurrencesOfString:@"RAC" withString:@""]]; } NSArray *sampleArray = [[[array.rac_sequence filter:^BOOL(NSString *string) { return [string hasPrefix:@"RAC"]; }] map:^(NSString *string) { return [string stringByReplacingOccurrencesOfString:@"RAC" withString:@""]; }] array]; //(Simple, Reactive, Functional, Programming) Imperative Functional NSArray *array = @[@"RACSimple", @"Imperative", @" ", @"RACReactive", @"RACFunctional", @"RACProgramming"];
  4. Что за ReactiveCocoa? ReactiveCocoa: 1. Open-source GitHub framework 2. Реализует

    парадигму FRP для iOS/OSX 3. 2.5 версия Objc, 3.0 версия Swift 4. Carthage / submodules / unofficial pod 5. RACStream
  5. RACSequence 1. Pull-driven stream 2. About working with collections RACSequence

    *letters = [@"A B C D E F G H I" componentsSeparatedByString:@" "].rac_sequence; // Contains: AA BB CC DD EE FF GG HH II RACSequence *mapped = [letters map:^(NSString *value) { return [value stringByAppendingString:value]; }];
  6. RACSignal 1. Push-driven stream of values 2. Events : Next,

    Completed, Error 3. One or more Subscribers 4. Operations: Subscribing, Side-effects, Transformation, Combining, Control 5. Signals are cold // RACObserve(self, username) creates a new RACSignal [RACObserve(self, username) subscribeNext:^(NSString *newName) { NSLog(@"%@", newName); }];
  7. Basic operators Subscriptions: subscribeNext subscribeCompleted subscribeError Side-effects: doCompleted doNext doError

    doDisposed Transforming: map filter Combining: concat merge flattenMap then combineLatest switchToLatest Control: skip takeUntil throttle
  8. Observe [RACObserve(self.viewModel, recipeName) subscribeNext:^(NSString *string) { //... }]; [[RACObserve(self.viewModel, recipeName)

    filter:^BOOL(NSString *string) { return (string.length > 10); }] subscribeNext:^(NSString *string) { NSLog(@"RecipeName : %@", string); //... }];
  9. Bindings RAC(self, titleLabel.text) = RACObserve(self, model.title); RAC(self.startButton, enabled) = RACObserve(self.viewModel,

    canStartTimer); RAC(self.viewModel, stepName) = self.stepNameTextField.rac_textSignal; RAC(self, titleLable.text) = [RACObserve(self, model.title) map:^id(NSString *text) { return [NSString stringWithFormat:@”Title: %@”, text]; }];
  10. Operations on collections RACSequence *goodPlayersArray = [[[playersArray rac_sequence] filter:^BOOL(MatchViewModel *match)

    { return (player.goals > 5); }] map:^id(Player *player) { return player.name; }]; NSArray *myArray = [NSArray new]; [myArray enumerateObjectsUsingBlock:^(id anObject, NSUInteger idx, BOOL *stop) { //... }];
  11. [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { __block NSURLSessionDataTask *task = [self

    GET:url parameters:parameters completion:^(id response, NSError *error) { if (!error) { [subscriber sendNext:response]; [subscriber sendCompleted]; } else { [subscriber sendError:error]; } }]; return [RACDisposable disposableWithBlock:^{ [task cancel]; }]; }]]; Networking
  12. Where to go from here? https://gist.github.com/JaviLorbada/4a7bd6129275ebefd5a6 The best FRP in

    iOS links. https://github.com/ReactiveCocoa/ReactiveCocoa/tree/core-legacy- objc/Documentation ReactiveCocoa Objective-C Documentation