ios - 如何在执行其他事件时忽略该事件?

标签 ios swift reactive-cocoa reactive-cocoa-3

假设您正在观察一个经常变化的属性,例如当队列低于阈值时应重新填充?

queue.rac_valuesForKeyPath("count", observer: self)
  .toSignalProducer()
  .filter({ (queueCount: AnyObject?) -> Bool in
     let newQueueCount = queueCount as! Int
     return newQueueCount < 10
  })
  .on(next: { _ in
     // Refilling the queue asynchronously and takes 10 seconds
     self.refillQueue(withItemCount: 20)
  })
  .start()

当队列为空时,将触发下一个处理程序并填充队列。在填充队列时,SignalProducer 会发送一个新的下一个事件,因为 count 属性更改为 1 – 又一个又一个。但我不希望触发下一个处理程序。相反,我希望每次队列低于该阈值时它都会触发一次。

我怎样才能以最好的方式做到这一点?有什么有用的事件流操作吗?有什么想法吗?

干杯,

赫拉尔多

最佳答案

我认为您可以在这里使用 combinePrevios 运算符,因为它为您提供当前值和以前的值:

queue.rac_valuesForKeyPath("count", observer: self)
  .toSignalProducer()
  .combinePrevious(0)
  .filter { (previous, current) -> Bool in
    return previous >= 10 && current < 10
  }
  .on(next: { _ in
     // Refilling the queue asynchronously and takes 10 seconds
     self.refillQueue(withItemCount: 20)
  })
  .start()

另一种方法是在原始代码中的 filter 之后添加 skipRepeats() ,但我认为在这种特殊情况下 combinePrevious 更明确.

关于ios - 如何在执行其他事件时忽略该事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35370444/

相关文章:

Objective-C block 和变量范围

ios - ios中如何根据方向改变图像位置?

ios - NSRangeException - 无法删除观察者

android - 同时编写调试和生产代码的最佳做法是什么?

ios - 将发出数组的 SignalProducer 转换为发出原始数组所有元素的 SignalProducer

ios - 'Person' 没有可见的@interface 声明选择器 'setEmployeeID:'

ios - 如何在avplayer中播放rtmp链接?

ios - 为什么 iOS 项目归档比构建需要更长的时间?

ios - 设置间隔时 QueueScheduler 不触发

ios - 编译 ReactiveCocoa 时出错