宽限期信号

时间:2014-10-06 17:22:45

标签: ios reactive-cocoa

当对象的属性isWorking设置为YES时,应在屏幕上显示活动指示符。此代码修复了:

[RACObserve(object, isWorking) subscribeNext:^(NSNumber *isWorking) {
  if ([isWorking boolValue]) {
    [activityIndicator startAnimating];
  }
  else {
    [activityIndicator stopAnimating];
  }
}];

除非经过一段时间,比如1秒,否则如何防止信号开火。这样可以防止活动指示灯闪烁。

在这种情况下,限制不会这样做吗?

1 个答案:

答案 0 :(得分:2)

当isWorking等于YES时,我会包装原始信号以引入延迟。像下面这样的东西。

RACSignal *isWorkingSignal = [[RACObserve(object, isWorking)
        map:^id(NSNumber *isWorking) {
            if (YES == [isWorking boolValue]) {
                NSTimeInterval delayInterval = 2; // seconds
                return [[RACSignal return:isWorking] delay:delayInterval];
            } else {
                return [RACSignal return:isWorking];
            }
        }] switchToLatest];

[isWorkingSignal subscribeNext:^(NSNumber *isWorking) {
    if (YES == [isWorking boolValue]) {
        [activityIndicator startAnimating];
    }
    else {
        [activityIndicator stopAnimating];
    }
}];