سؤال

I am trying to merge NSNotifications that happens within a short amount of time. I tried the following:

[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:kMyNotificationName object:self]postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];

It looks like my notifications are nicely merged but they are sent only when a UI interaction happens. For example, I enqueue many notifications but they are fired only when I touch the tableView of my current view controller. How to fire them even though there is no UI interaction?

Edit
I tried different posting style : NSPostNow (or NSPostASAP) doesn't do what I need (my notifications are not merged)

هل كانت مفيدة؟

المحلول

See the edit

I have no experience with this mechanism (thanks for making me discover it ;)), but I think your are looking for the posting style NSPostNow.

From the docs:

NSPostNow

The notification is posted immediately after coalescing.

So your code may look like this:

[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:kMyNotificationName object:self] postingStyle:NSPostNow coalesceMask:NSNotificationCoalescingOnName forModes:nil];

EDIT: After reading the docs more thoroughly, (in particular NotificationQueues and RunLoopManagement), I think your issue is that your are posting notifications only in default run loop mode.

From my understanding and the tests I made, you should use the posting style NSPostingASAP (NSPostingNow is in fact equivalent to calling postNotification on the NSNotificationCenter, that is not what you want) and post for all common run loop modes (ie. NSRunLoopCommonModes).

This will ask the queue to post notification ASAP, even during the NSEventTrackingRunLoopMode loop mode (included in NSRunLoopCommonModes).

[[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:kMyNotificationName object:self] postingStyle:NSPostASAP coalesceMask:NSNotificationCoalescingOnName forModes:NSRunLoopCommonModes];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top