Re: creating multiple NSTimers
Re: creating multiple NSTimers
- Subject: Re: creating multiple NSTimers
- From: Conrad Shultz <email@hidden>
- Date: Thu, 29 Sep 2011 17:29:33 -0700
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 9/29/11 4:48 PM, Tom Hohensee wrote:
> Sorry, I started this out wrong. I am probably over thinking
> this to the point of confusion. Here is where I am. I have an
> application for a facility that uses a number of ip enabled set top
> boxes to drive TV's throughout the place. Each box is to be
> shutdown at certain times of the day depending on what part of the
> facility is closed for the day. In my app the user sets up a
> configuration in an NSTable whereby each device is set to shut down
> at a certain time each day. From this configuration, I setup
> timers to correspond to the shutdown time and have them repeat each
> 24 hours. I originally thought I would simply load each into the
> runloop using NSTimers class method
> +scheduledTimerWithTimeInterval:target:selector:userinfo:repeats.
> without retaining a reference to it. But I ran into the problem
> of the user changing a device's shutdown time while the app is
> running. I needed to retain a reference to the timer to invalidate
> it . In other words, if the user comes along and wants to change
> the configuration (a shut down time) of one device while the app
> is running I need to be able to stop the timer that is currently in
> the run loop and use the new configuration. Without invalidating
> that timer would I not have an active timer in the run loop on top
> of what is created by new configuration? I then moved on the
> creating timers and instead of loading them into the run loop I
> would load them into an array and and sort them according to time.
> I then loop through the array loading only the first timer of the
> array into the run loop and retain a reference to it. Each firing
> of the timer loads the next timer into the run loop and retains a
> reference to it. But this has other issues particularly when firing
> times are real close together.
>
> At this point I need a fresh prospective on this. Any thoughts
> would be greatly appreciated.
OK, from the description I still don't see why you couldn't schedule
the timers on the run loop and keep references to them in an
appropriately keyed dictionary. When someone needs to edit the
shutdown time you simply fetch the timer from the dictionary and
- -setFireDate:. You would also have the ability to invalidate timers
and remove them from the dictionary.
Just make sure you remove the timers from the dictionary once
fired/invalidated. (You could probably avoid this step by using
zeroing weak references if you really wanted.)
The polling approach described by John and Jamie would presumably also
work, but I would consider the dictionary approach more straightforward.
- --
Conrad Shultz
Synthetiq Solutions
www.synthetiqsolutions.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iD8DBQFOhQ1saOlrz5+0JdURAvEhAKCK7mCy5d/Y2Byx3CrKGPtyJ2PgowCffXNt
oCPRXWNVcHVyCLeRIuGQSyI=
=dH8W
-----END PGP SIGNATURE-----
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden