Reputation: 295
I am developing an iPhone app (in Objective-C), and I am trying to make it so that there is some sort of 'action listener' that is constantly checking to see if the current date and time match that of the first object in a NSMutableArray
filled with event objects (retrieved from the users calendar).
My problem is that I cannot figure out how to do so, while the rest of my code continues to run, because right now I have a for
loop running to constantly check, which stops my code, and prevents the app from continuing to run. If anyone knows any sort of tutorials, or has an explanation, I would greatly appreciate it.
Thanks!
Upvotes: 0
Views: 468
Reputation: 5703
You could use an NSTimer
EDIT: see phix23's answer for a more reliable "fireDate" code for use with iOS.
Apple Documentation: NSTimer
Upvotes: 0
Reputation: 4452
get the time between the current time and the event time, and use dispatch_after
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, timeDifference * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//Your code
});
Upvotes: 0
Reputation: 35394
Setup a NSTimer to fire at the desired date. This NSTimer category here is useful for this purpose: https://github.com/adamjernst/NSTimer-AbsoluteFireDate
You can also try to create and schedule a UILocalNotification to get notified at a specific date.
Upvotes: 1