Objective c 使用ReactiveCocoa枚举对象之间有延迟的数组
我目前正在做类似的事情,在数组被修改时逐步迭代数组:Objective c 使用ReactiveCocoa枚举对象之间有延迟的数组,objective-c,reactive-cocoa,Objective C,Reactive Cocoa,我目前正在做类似的事情,在数组被修改时逐步迭代数组: [[[[RACObserve(self, match.moves) combinePreviousWithStart:@[] reduce:^id(NSArray * previous, NSArray * current) { NSArray * newMoves = current; if (previous.count > 0) { newMoves = _.tail
[[[[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
在回答了几个不太正确的问题后,你可以做一个改变,这将(我认为)真正做到你所要求的:
RACSignal *emptyDelay = [[RACSignal empty] delay:1];
RACSequence *delayedMoves = [newMoves.rac_sequence map:^(CCXMove *move) {
return [emptyDelay concat:[RACSignal return:move]];
}];
return [RACSignal concat:delayedMoves];
您可以将第7-10行简化为
return[RACSignal return:move]代码>。JFYI@erikprice谢谢你的指针。我已经更新了上面的代码示例,将-flattmap与RACReturnSignal一起使用。这可以正确地处理每次向数组添加单个对象时的延迟,但是使用类似这样的更新仍然会在第一个对象之后立即触发第二个新的next
:self.match.moves=[self.player.match.moves arrayByAddingObjectsFromArray:@[@1,@2]
aaa又一次尝试。太棒了,你最新更新中的EmptyDisplay
方法正是我想要的。谢谢!