2

How to post NSNotification with delay.

One solution that is in my mind :- post notification in performSelectorAfterDelay method. Is there any better solution for this.

Is NSNotificationQueue can help me to achieve this ?

pkc456
  • 8,350
  • 38
  • 53
  • 109
  • see this link may be helps you http://stackoverflow.com/questions/17171352/calling-method-with-delay-in-main-thread – Anbu.Karthik Feb 18 '16 at 06:46

2 Answers2

13

Make use of GCD's dispatch_after() method. In Objective-C, this would look like:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [[NSNotificationCenter defaultCenter] postNotification:someNotification];
});

Update: Swift 3

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(5)) { 
    NotificationCenter.default.post(someNotification)
}
ZeMoon
  • 20,054
  • 5
  • 57
  • 98
0

You can also use NSLocalNotification and set a fireDate. The main difference is that with NSLocalNotification, the user will see the notification when the app is in background ( like it is a RemoteNotification).

It could be useful if you notification is a Reminder or that kind of thing.

CZ54
  • 5,488
  • 1
  • 24
  • 39