Re: Monitoring system time
Re: Monitoring system time
- Subject: Re: Monitoring system time
- From: John Timmer <email@hidden>
- Date: Mon, 30 May 2005 12:28:48 -0400
>> Could anyone give me information on the best way to monitor the
>> system date/time and trigger an event?
>> Basically I have some alarm records that have date and time stored -
>> when the system date/time matches one of the records I need to fire
>> an event.
>
> NSTimer is nice and all, but if 1) the computer sleeps 2) the user
> changes the date in sys prefs 3) the computer adjusts the time for
> daylight savings time then your NSTimers will need to be adjusted. So
> you must watch for wake and date change notifications.
This is a key point - the behavior of NSTimers when a system has been
sleeping past the time they should have been triggered is undefined. What I
tend to do is have any object that needs to perform an activity on a
periodic basis register for sleep notifications and target its own periodic
execution using NSObject's "performSelector:withObject:afterDelay:" method.
When the system goes to sleep, clear this timer using
"cancelPreviousPerformRequestsWithTarget:" and register for wake
notifications. When you wake up, you can determine whether you should fire
the events (if you've slept past them) or recalibrate your timer based on
the new time.
If you have a lot of objects of different classes that would need to fire
off periodic activities, it might be worth making a singleton wake/sleep
object to handle this, so that you can consolidate the code.
JT
_______________________________________________
This mind intentionally left blank
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden