• 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
Rescheduling an NSTimer from a completion method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Rescheduling an NSTimer from a completion method


  • Subject: Rescheduling an NSTimer from a completion method
  • From: Fritz Anderson <email@hidden>
  • Date: Tue, 11 Aug 2009 12:28:53 -0500

iPhone OS 3.0

Can an NSTimer be rescheduled after firing, and after another trip through the run loop?

I'd like to "reschedule" a NON-repeating timer in my own code. I assume that in the simple case it would be something like:

- (void) handleTimer: (NSTimer *) aTimer
{
// ... do things ...
[aTimer setFireDate: someTimeInTheFuture];
[[NSRunLoop currentRunLoop] addTimer: aTimer forMode: NSDefaultRunLoopMode];
}


I'm pretty sure that this is legal — am I right?

Now for the non-simple case. I want the timer to restart from the dismissal of a UIAlertView. I have the timer retained in an instance variable in addition to having it scheduled in a run loop. Can I do this:

- (void) handleTimerAndAlert: (NSTimer *) aTimer
{
	NSParameterAssert(aTimer == self.myTimerIvar);
	//	... do things ...
	UIAlertView *	anAlert = [[UIAlertView alloc]
					initWithTitle: @"Hello World"
					message: @""
					delegate: self
					cancelButtonTitle: @"OK"
					otherButtonTitles: nil];
	[anAlert show];
	[anAlert release];
}

- (void) alertView: (UIAlertView *) alertView
didDismissWithButtonIndex: (NSInteger) buttonIndex
{
// Called many, many run-loop iterations later
self.myTimerIvar.fireDate = [NSDate dateWithTimeIntervalSinceNow: 10.0];
[[NSRunLoop currentRunLoop] addTimer: self.myTimerIvar forMode: NSDefaultRunLoopMode];
}


Will this work? I seem to remember (but I can't find it) that after a non-repeating NSTimer fires, NSRunLoop sends it -[NSTimer invalidate], which does destructive things beyond simply releasing the timer from its queue. I worry that passing to another iteration of the run loop will make it impossible to reschedule the timer.

I _could_ try this myself, but the process is apt to be subtle and depend on non-reproducible conditions. Therefore I'd like the benefit of others' experience. My search of CocoaBuilder (NSTimer NSRunLoop reschedule) turns up only explanations of how repeating timers are rescheduled.

Or should I just release and nil-out self.myTimerIvar, and schedule a new one in the alert's completion method? It seems to me that the possibility of more than one timer for a single queue can only lead to heartache.

	— F

_______________________________________________

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: Rescheduling an NSTimer from a completion method
      • From: Christopher Kane <email@hidden>
  • Prev by Date: Re: NSUserDefault and Negative numerical arguments (Was: Posting mouse clicks with multiple displays)
  • Next by Date: Re: converting unicode text representation to unichar
  • Previous by thread: Re: UITabBarController Confusion
  • Next by thread: Re: Rescheduling an NSTimer from a completion method
  • Index(es):
    • Date
    • Thread