ATV
ATV

Reputation: 4536

Coalescing NSNotifications

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.

Upvotes: 3

Views: 239

Answers (1)

Pierre Bernard
Pierre Bernard

Reputation: 3198

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.

Upvotes: 1

Related Questions