简体   繁体   English

当执行其他事件时如何忽略事件?

[英]How to disregard events when other event is executed?

let's say you are observing a property that changes quite often, eg a queue that should be refilled when it is below a threshold? 假设您正在观察一个经常更改的属性,例如,当队列低于阈值时应重新填充的队列?

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()

When the queue is empty, the next handler will be triggered and fills the queue. 当队列为空时,将触发下一个处理程序并填充队列。 While filling the queue, the SignalProducer sends a new next event because the count property changed to 1 – and another and another. 在填充队列时,SignalProducer发送一个新的下一个事件,因为count属性更改为1 –一个又一个。 But I do not want the next handler to be triggered. 但是我不希望触发下一个处理程序。 Instead, I'd like it trigger once everytime the queue falls below that threshold. 相反,我希望每次队列低于该阈值时触发一次。

How can I do this in the best way? 我怎样才能最好地做到这一点? Are there any helpful event stream operations? 是否有任何有用的事件流操作? Any ideas? 有任何想法吗?

Cheers, 干杯,

Gerardo 杰拉多

I think you can use combinePrevios operator here, as it gives you the present and the previous value: 我认为您可以在此处使用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()

Another approach would be adding skipRepeats() after filter in your original code, but I think combinePrevious is more explicit in this particular case. 另一种方法是在原始代码中的filter之后添加skipRepeats() ,但我认为在这种特殊情况下combinePrevious更加明确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在最清晰的uiview中忽略顶级事件,而其他uiview可以处理它们 - how to disregard touch events in topmost uiview when it is clear and a different uiview can handle them 仅当触摸类型为手指时,如何将触摸事件传递给其他视图? - How to pass touch event to other views only when touchtype is finger? 在iOS中执行onkeyup事件时,javascript focus()无法正常工作 - Javascript focus() not working when onkeyup event is executed in iOS 当UIPanGestureRecognizer出现在UIScrollView旁边时,如何取消其他事件? - How can I cancel other events when my UIPanGestureRecognizer occurs along side a UIScrollView? 如何接收在 iOS 上的 WKWebView 内执行的 Javascript 中的在线/离线事件? - How to receive online/offline events in Javascript that's executed inside WKWebView on iOS? 如何将触摸事件传递给其他视图? - How to pass a touch event to other views? 如何禁用 ios-charts 中的交叉和其他触摸事件? - How to disable the cross and other touch events in ios-charts? 如何停止3d触摸中断其他触摸事件? - How to stop 3d touch its interrupt other touch events? 单击日历上的日期时如何显示事件 - How to display events when clicking a date on calendar iOS Swift:如何使 UILabel 字体大小静态(忽略 iOS 设置字体大小更改)? - iOS Swift: How to make UILabel font size static (disregard of iOS setting font size change)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM