我目前正在做这样的事情,以便在修改数组时逐步遍历数组:
[[[[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]);
}];
然而,看起来延迟工作的方式是当前next
调用只会延迟1秒而不是在前一次执行完成后至少1秒内发生的每个next
的预期效果。输出:
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
…
正在修改的阵列和第一个下一个呼叫之间有1秒的延迟,但随后所有后续呼叫都是立即呼叫而不是延迟。
为后代编辑,Dave Lee的帮助下面是一个有效的解决方案:
[[[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]];
输出:
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
答案 0 :(得分: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];