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

ReactiveCocoaでのフロー制御の例

 ReactiveCocoaでのフロー制御の例

ReactiveCocoa 2.xを使ったサンプルアプリでのフロー制御例を紹介します。

matuyuji

July 26, 2014
Tweet

More Decks by matuyuji

Other Decks in Programming

Transcript

  1. ΍͍ͬͯΔ͜ͱ w +40/औಘ  " IUUQTBQJJJKNJPKQNPCJMFEWDPVQPO  # IUUQTBQJJJKNJPKQNPCJMFEWMPHQBDLFU w

    +40/σʔλͷϚʔδ w 0"VUIʹΑΔೝূ τʔΫϯ࠶औಘ → AFNetworking-RACExtensionsͷ rac_startJSONRequestOperationWithRequest: → concat:ͰͱΓ͚͋͑ͣͬͭ͘Δɻ map:಺ͰMantle࢖͔ͬͯΒϚʔδ → ΞΫηετʔΫϯΛnilʹͯ͠࠶࣮ߦ
  2. +40/σʔλͷऔಘ (A) (B) rac_startJSONRequest…: defer defer rac_startJSONRequest…: concat: collect NSDict

    NSDict NSDict × 2 NSArray → AFNetworking-RACExtensionsͷ rac_startJSONRequestOperationWithRequest:
  3. +40/σʔλͷऔಘ - (RACSignal*)loadInformationSignal { RACSignal* coupon = [RACSignal defer:^RACSignal* {

    return [self.restHelper getCoupon]; }]; ! RACSignal* packet = [RACSignal defer:^RACSignal* { return [self.restHelper getPacket]; }]; ! RACSignal* sig = [[coupon concat:packet] collect]; return sig; } தͰrac_startJSONRequestOperationWithRequest:ΛݺͿ
  4. 0"VUIͷೝূ - (RACSignal*)loadInformationSignal { RACSignal* coupon = [RACSignal defer:^RACSignal* {

    return [self.restHelper getCoupon]; }]; ! RACSignal* packet = [RACSignal defer:^RACSignal* { return [self.restHelper getPacket]; }]; ! RACSignal* sig = [[coupon concat:packet] collect]; return self.restHelper.accessToken ? sig : [[[self.restHelper authorize] catch:self.errorBlock] concat:sig]; } complete͔error͚ͩฦ͢γάφϧ
  5. 0"VUIͷೝূ (A) (B) rac_start…: defer defer concat: collect NSDict NSDict

    NSDict × 2 NSArray rac_start…: concat: authorize catch: NSArray
  6. MPBE*OGPSNBUJPO4JHOBM (A) (B) rac_start…: defer defer concat: collect NSDict NSDict

    NSDict × 2 NSArray rac_start…: concat: authorize catch: NSArray
  7. MPBE*OGPSNBUJPO4JHOBM - (RACSignal*)loadInformationSignal { RACSignal* coupon = [RACSignal defer:^RACSignal* {

    return [self.restHelper getCoupon]; }]; ! RACSignal* packet = [RACSignal defer:^RACSignal* { return [self.restHelper getPacket]; }]; ! RACSignal* sig = [[coupon concat:packet] collect]; return self.restHelper.accessToken ? sig : [[[self.restHelper authorize] catch:self.errorBlock] concat:sig]; }
  8. ϚʔδͱτʔΫϯ࠶औಘ [[[self loadInformationSignal] catch: ^RACSignal *(NSError *error) { if (/*ΞΫηετʔΫϯ͕ແޮ*/)

    { self.restHelper.accessToken = nil; return [self loadInformationSignal]; } return [RACSignal empty]; //ଞͷΤϥʔ͸͖͋ΒΊΔ }] map:^(NSArray* array) { // MantleͰΦϒδΣΫτ࡞੒͔ͯ͠ΒɺσʔλΛ·ͱΊΔ }];
  9. τʔΫϯ࠶औಘ (A) (B) rac_start…: defer defer concat: collect NSDict NSDict

    NSDict × 2 NSArray rac_start…: concat: authorize catch: NSArray
  10. ΍ͬͨ͜ͱ w +40/औಘ  " IUUQTBQJJJKNJPKQNPCJMFEWDPVQPO  # IUUQTBQJJJKNJPKQNPCJMFEWMPHQBDLFU w

    +40/σʔλͷϚʔδ w 0"VUIʹΑΔೝূ τʔΫϯ࠶औಘ → AFNetworking-RACExtensionsͷ rac_startJSONRequestOperationWithRequest: → concat:ͰͱΓ͚͋͑ͣͬͭ͘Δɻ map:಺ͰMantle࢖͔ͬͯΒϚʔδ → ΞΫηετʔΫϯΛnilʹͯ͠࠶࣮ߦ
  11. ࢖ͬͨϥΠϒϥϦ w 3FBDUJWF$PDPB w "'/FUXPSLJOH3"$&YUFOTJPOT w 6OEFSTDPSFN w .BOUMF w

    58.FTTBHF#BS.BOBHFS w 75"DLOPXMFEHFNFOUT7JFX$POUSPMMFS w 9:1JF$IBSU w J041MPU