gpt4 book ai didi

objective-c - 使用 ReactiveCocoa 枚举对象之间有延迟的数组

转载 作者:搜寻专家 更新时间:2023-10-30 20:02:34 26 4
gpt4 key购买 nike

我目前正在做这样的事情来逐步遍历一个数组,因为它被修改了:

    [[[[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 秒,而不是每个 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

在数组被修改和第一次下一次调用之间有 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];

关于objective-c - 使用 ReactiveCocoa 枚举对象之间有延迟的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22792802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com