Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate number of dynamic NSTimers not repeating on very first load, fine after that
    primarykey
    data
    text
    <p>I am creating a number (~5) NSTimer instances from data stored in a SQLite database using the following code below:</p> <pre><code>-(void)setupNotificationTimer:(NSDictionary *)timerDetails{ NSLog(@"TIMER REPEATS VALUE: %@", [timerDetails objectForKey:@"repeats"]); NSLog(@"INTERVAL: %f", [[timerDetails objectForKey:@"interval"] floatValue]); bool repeat = [[timerDetails objectForKey:@"repeats"] boolValue]; if (repeat) { NSLog(@"Should Repeat"); } else{ NSLog(@"Should Not Repeat"); } NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:[[timerDetails objectForKey:@"interval"] floatValue] target:self selector:@selector(fireTimer:) userInfo:timerDetails repeats:repeat]; [timer fire]; [timers addObject:timer]; //'timers' is a property of the AppDelegate } -(void)fireTimer:(NSTimer *)timer { NSLog(@"Timer Fired"); NSMutableDictionary *dict = [timer userInfo]; [[NSNotificationCenter defaultCenter] postNotificationName:[dict objectForKey:@"notificationName"] object:nil]; } </code></pre> <p>When I setup these timers the very first time the app loads the timer is setup correctly as the NSLog output below shows but does not repeat despite clearly being set to repeat. Each timer fires once then does not continue.</p> <pre><code>2013-04-03 11:12:53.541 Test[2635:752b] TIMER REPEATS VALUE: 1 2013-04-03 11:12:53.542 Test[2635:752b] INTERVAL: 10.000000 2013-04-03 11:12:53.543 Test[2635:752b] Should Repeat 2013-04-03 11:12:53.544 Test[2635:752b] Timer Fired </code></pre> <p>The strange thing happens when I close the app and then re-open it, the timers are created and actually fire repeatedly. This app is also a universal iPhone and iPad app and I only seem to get this behaviour when running the app on iPhone. I really am stumped by this and any help is appreciated.</p> <p><strong>Edit</strong></p> <p>As requested the code to cancel the timers:</p> <pre><code>-(void)viewWillDisappear:(BOOL)animated{ NSArray *viewControllersSet = self.navigationController.viewControllers; if (viewControllersSet.count &gt; 1 &amp;&amp; [viewControllersSet objectAtIndex:viewControllersSet.count-2] == self) { // View is disappearing because a new view controller was pushed onto the stack NSLog(@"New view controller was pushed"); } else if ([viewControllersSet indexOfObject:self] == NSNotFound) { // View is disappearing because it was popped from the stack NSLog(@"View controller was popped"); for(int i = 0; i &lt; [[appDelegate timers] count]; i ++){ NSTimer *timer = [[appDelegate timers] objectAtIndex:i]; [timer invalidate]; timer = nil; } [[appDelegate timers] removeAllObjects]; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload