3

How might one get a function to be called at given time and date? For example, I would like to store some values of some variables at midnight (and clear them to zero) so they can be compared with the previous days values.

How might I accomplish this in Objective-C?

Thanks,

Jack

ChikabuZ
  • 10,031
  • 5
  • 63
  • 86
Jack Nutkins
  • 1,555
  • 5
  • 36
  • 71
  • Will your app be running at midnight? The only reliable option is to have a server running on Microsoft Azure or something, which will execute the task at midnight. You can't assume your app will be launched at midnight on the device. – Abhi Beckert May 18 '14 at 17:13

2 Answers2

2

You've got quite a few options, depending on what you're after.

The obvious place to start is NSTimer.

There's also the performSelector withDelay function family.

Lastly, a cool extension I sometimes use, delayed blocks.

Oh, one more - you can also setup Local Notifications.

Peter Sarnowski
  • 11,900
  • 5
  • 36
  • 33
2

Use NSTimer's initWithFireDate:interval:target:selector:userInfo:repeats: to set the time you want a method to be run.

TheAmateurProgrammer
  • 9,252
  • 8
  • 52
  • 71