dev.nikolaz dev.nikolaz - 23 days ago 6
iOS Question

Get RACTuple from merged signals

I don't have any idea how to merge a lot of signals and get results from a RACTuple, its seems to be like easy answer but I can't found that.

What we have for exmaple:

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"}];


and

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);
}];


this operator
map
is wrong I know that, but this is for example

- (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;
}];
}


In NSLog I want to see tuple result with two arrays first - s1, second - s2
Thx

Answer

I've written a small example, hope it helps you.

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
}];