使用ReactiveCocoa枚举对象之间延迟的数组

Cla*_*ris 3 objective-c reactive-cocoa

我正在做这样的事情,以便在修改时逐步遍历数组:

    [[[[RACObserve(self, match.moves) combinePreviousWithStart:@[] reduce:^id(NSArray * previous, NSArray * current) {
        NSArray * newMoves = current;
        if (previous.count > 0) {
            newMoves = _.tail(current, current.count - previous.count);
        }
        return [newMoves.rac_sequence.signal flattenMap:^RACSignal*(CCXMove * move) {
            return [RACSignal return:move];
        }];
    }] concat] delay:1] subscribeNext:^(id move) {
        @strongify(self);
        NSLog(@"next %lu called", (unsigned long)[self.match.moves indexOfObjectIdenticalTo:move]);
    }];
Run Code Online (Sandbox Code Playgroud)

然而,看起来像延迟工作的方式是当前next调用将被延迟1秒而不是next在前一次执行完成后至少1秒发生的每个期望的效果.输出:

    2014-04-01 21:38:15.820 RACPlayground[74040:60b] self.match.moves updated
    2014-04-01 21:38:16.823 RACPlayground[74040:1303] next 0 called
    2014-04-01 21:38:16.824 RACPlayground[74040:1303] next 1 called
    2014-04-01 21:38:16.824 RACPlayground[74040:1303] next 2 called
    …
Run Code Online (Sandbox Code Playgroud)

被修改的阵列和第一个下一个呼叫之间有1秒的延迟,但随后的所有后续呼叫都是立即的而不是被延迟.

编辑为后代,戴夫李的帮助下面的工作解决方案如下:

    [[[RACObserve(self, match.moves) combinePreviousWithStart:@[] reduce:^id(NSArray * previous, NSArray * current) {
        NSArray * newMoves = current;
        if (previous.count > 0) {
            newMoves = _.tail(current, current.count - previous.count);
        }
        RACSignal *emptyDelay = [[RACSignal empty] delay:1];
        RACSequence *delayedMoves = [newMoves.rac_sequence map:^(CCXMove *move) {
            return [emptyDelay concat:[RACSignal return:move]];
        }];
        return [RACSignal concat:delayedMoves];
    }] concat] subscribeNext:^(CCXMove * move) {
        @strongify(self);
        NSLog(@"processing move %lu", (unsigned long)[self.match.moves indexOfObjectIdenticalTo:move]);
    }];

    NSLog(@"appending a two objects one at a time");
    self.match.moves = [self.match.moves arrayByAddingObject:@1];
    self.match.moves = [self.match.moves arrayByAddingObject:@2];

    NSLog(@"appending two objects at the same time");
    self.match.moves = [self.match.moves arrayByAddingObjectsFromArray:@[@3, @4]];
Run Code Online (Sandbox Code Playgroud)

输出:

    2014-04-01 23:31:34.042 RACPlayground[79495:60b] appending a two objects one at a time
    2014-04-01 23:31:34.044 RACPlayground[79495:60b] appending two objects at the same time
    2014-04-01 23:31:35.044 RACPlayground[79495:1303] processing move 0
    2014-04-01 23:31:36.045 RACPlayground[79495:1303] processing move 1
    2014-04-01 23:31:37.046 RACPlayground[79495:1303] processing move 2
    2014-04-01 23:31:38.047 RACPlayground[79495:1303] processing move 3
Run Code Online (Sandbox Code Playgroud)

Dav*_*Lee 5

经过几个不太正确的答案之后,这是一个你可以做出的改变(我认为)实际上你做了什么:

RACSignal *emptyDelay = [[RACSignal empty] delay:1];
RACSequence *delayedMoves = [newMoves.rac_sequence map:^(CCXMove *move) {
    return [emptyDelay concat:[RACSignal return:move]];
}];
return [RACSignal concat:delayedMoves];
Run Code Online (Sandbox Code Playgroud)