3

Is there any way when using coalescing notifications on NSNotificationQueues to control the time range?

I would like to coalesce e.g. for the next second, but as I understand the options available it's either immediately or on next run loop invocation or 'when idle'..

Any other way to e.g. coalesce over a longer time range?

I'm basically looking for a way to gather all user events (like pinch-to-zoom) over a small time range and launch an expensive operation only after the user apparently stopped zooming/etc.

ATV
  • 4,116
  • 3
  • 23
  • 42

1 Answers1

1

You can schedule a NSTimer when the first notification comes in. When the next notification comes in, check your timer instance variable. If it is non-nil, ignore the notification. Otherwise start a fresh timer.

When the timer fires, clear your timer instance variable and launch your expensive operation.

An simpler alternative is to use performSelector:afterDelay: and cancelPreviousPerformRequestsWithTarget:selector:object:. The effect however is not the same. You will further delay your expensive operation each time a notification is received. If notifications come faster than the delay you set, the expensive operation will never run.

Pierre Bernard
  • 3,148
  • 2
  • 23
  • 31
  • Right, so you're describing a 100% custom implementation. Sure, that's one way to do it - but not related to using the built-in coalescing notifications mechanism.. – ATV Jul 25 '16 at 12:03
  • 1
    Yes. I don’t think the built-in coalescing provides anything other than “end of runloop” or “when idle”. This is good enough for most cases. If you wish to have your expensive operation triggered no more than once per second, you probably have to implement that using a timer. – Pierre Bernard Jul 25 '16 at 13:25