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

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

然而,延迟的工作方式看起来是当前的 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/

相关文章:

ios - 使用 ReactiveCocoa 在模型更新时查看 Controller 状态更新

ios - ReactiveCocoa rac_valuesForKeyPath 在 Swift 中不起作用

ios - ReactiveSwift 中运算符 '<~' 的使用不明确

validation - ReactiveCocoa - 观察 isFirstResponder 属性和 UITextField 并将clearsOnBeginEditing 设置为 YES

objective-c - 在 ReactiveCocoa 中,subscribeNext 和 subscribeCompleted 有约定吗?

objective-c - Cocoa中不按按钮触发回调

ios - QLPreviewController 在呈现模态视图 Controller 后失去联系

ios - 使用 CGRectIntersectsRect 更新我的游戏分数

iphone - 将 UIView 添加到 cell.contentView 时出现 UITableView 性能问题

objective-c - NSManagedObject -managedObjectContext "return nil if the receiver has been deleted from its context"什么时候出现?