• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Best pattern to follow for scheduling an event
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Best pattern to follow for scheduling an event


  • Subject: Best pattern to follow for scheduling an event
  • From: John Stiles <email@hidden>
  • Date: Thu, 8 Nov 2007 10:05:58 -0800

I have a method that needs to schedule a "cleanup pass" to occur in the near future. The method might be called once, ten times, or a hundred times in a row, but I only need to clean up one time. To implement this, I used the following pattern, and I'm wondering if it was the best way to go.

First, when the object is first created, I create a timer. I scheduled its fire date to be in the distant, distant future:

    m_deferredFixupTimer = [[NSTimer
        scheduledTimerWithTimeInterval:DBL_MAX
                                target:myObject
                              selector:@selector(doFixUp:)
                              userInfo:NULL
                               repeats:YES] retain];

(I considered using INFINITY instead of DBL_MAX, but sometimes library code isn't prepared to deal with infinite values, so I figured DBL_MAX is safer and already more than large enough.)
Then, when my method is called, I schedule the timer to fire immediately:


    [m_deferredFixupTimer setFireDate:[NSDate date]];

This defers the fire to the next run-through of the event loop, which for my purposes is sufficient. It only fires one time.

So is this the right way to do it? It seems to work in practice, it just seemed a little quirky to me for some reason. I guess it would make more sense if I could make a timer that's scheduled to never fire, instead of a timer that's scheduled to repeat and fire once every trillion years. But in practice it seems to be equivalent.


_______________________________________________

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


  • Follow-Ups:
    • Re: Best pattern to follow for scheduling an event
      • From: Michael Nickerson <email@hidden>
    • Re: Best pattern to follow for scheduling an event
      • From: David Spooner <email@hidden>
    • Re: Best pattern to follow for scheduling an event
      • From: glenn andreas <email@hidden>
    • Re: Best pattern to follow for scheduling an event
      • From: j o a r <email@hidden>
    • Re: Best pattern to follow for scheduling an event
      • From: Jean-Daniel Dupas <email@hidden>
  • Prev by Date: Re: How to have an app/window always show up in the visible space
  • Next by Date: Re: dot syntax issue? (Newb Question?)
  • Previous by thread: Re: How to have an app/window always show up in the visible space
  • Next by thread: Re: Best pattern to follow for scheduling an event
  • Index(es):
    • Date
    • Thread