2016-11-15 39 views
0

我不知道如何合併很多信號並從RACTuple獲得結果,它似乎像是簡單的答案,但我找不到。從合併信號獲取RACTuple

我們有〔實施例什麼:

NSArray *a = @[@{@"k1":@"v1"}, 
       @{@"k2":@"v2"}, 
       @{@"k3":@"v3"}, 
       @{@"k4":@"v4"}, 
       @{@"k5":@"v5"}, 
       @{@"k6":@"v6"}, 
       @{@"k7":@"v7"}]; 
NSArray *b = @[@{@"kk1":@"vv1"}, 
       @{@"kk2":@"vv2"}, 
       @{@"kk3":@"vv3"}, 
       @{@"kk4":@"vv4"}, 
       @{@"kk5":@"vv5"}, 
       @{@"kk6":@"vv6"}, 
       @{@"kk7":@"vv7"}]; 

 RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) { 
      RACSignal *s1 = [self adaptObjects:a]; 
      RACSignal *s2 = [self adaptObjects:b]; 
      return [[RACSignal merge:@[s1,s2]] map:^id(id value) { 
       return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { 
        [subscriber sendNext:value]; 
        return nil; 
       }]; 
      }]; 
     }]; 

     [[command execute:nil] subscribeNext:^(RACTuple *x) { 
      NSLog(@"%@",x); 
     }]; 

該運營商map是錯了,我知道,但這是例如

- (RACSignal *)adaptObjects:(NSArray *)objects { 
    return [objects.rac_sequence.signal flattenMap:^RACStream *(id x) { 
     return [self adaptObject:x]; 
    }]; 
} 

- (RACSignal*)adaptObject:(NSDictionary*) x { 
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { 

     // some operations with data here 

     [subscriber sendNext:x]; 

     return nil; 
    }]; 
} 

在的NSLog我想首先看兩個數組的元組結果 - s1,second - s2 Thx

回答

3

我寫了一個小例子,希望它可以幫助你。

NSArray *a = @[@{@"k1":@"v1"}, 
       @{@"k2":@"v2"}, 
       @{@"k3":@"v3"}, 
       @{@"k4":@"v4"}, 
       @{@"k5":@"v5"}, 
       @{@"k6":@"v6"}, 
       @{@"k7":@"v7"}]; 

NSArray *b = @[@{@"kk1":@"vv1"}, 
       @{@"kk2":@"vv2"}, 
       @{@"kk3":@"vv3"}, 
       @{@"kk4":@"vv4"}, 
       @{@"kk5":@"vv5"}, 
       @{@"kk6":@"vv6"}, 
       @{@"kk7":@"vv7"}]; 

- (NSArray<RACSignal *> *)rac_signalsFromArray:(NSArray *)array { 
    NSMutableArray<RACSignal *> *signals = [NSMutableArray array]; 
    for (NSDictionary *dict in array) { 
     [signals addObject:[RACSignal return:dict]]; 
    } 
    return signals; 
} 

NSArray *Asignals = [self rac_signalsFromArray:a]; 
NSArray *Bsignals = [self rac_signalsFromArray:b]; 

NSArray *signals = [[NSArray arrayWithArray:Asignals] arrayByAddingObjectsFromArray:Bsignals]; 

[[RACSignal zip:signals] subscribeNext:^(RACTuple *tuple) { 
    // tuple here 
}]; 
+0

是的,它很好的例子。我錯過了我們可以用'return:'方法將任何對象包裝到RACSignal的事實。謝謝 –