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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to