假设您正在观察一个经常变化的属性,例如当队列低于阈值时应重新填充?
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/