我不知道如何合并大量信号并从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 谢谢
答案 0 :(得分: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
}];