Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timer Loop crashes sporadically after returning from the background

Tags:

iphone

ios6

I'm having a consistent problem in my latest app release. I have a timer that runs in the AppDelegate that calls a function every 30 seconds to load a new advertisement. I'm thinking it's the culprit in this crash. Using Crittercism, I've had over 20 crashes for 13 users. Users are mostly using IOS 6 or some variation of it. Here is the log that it's giving me:

SEGV_ACCERR

0 libobjc.A.dylib 0x3acd25b0 objc_msgSend + 16

1 Foundation 0x3394b277 __NSFireDelayedPerform + 451

2 CoreFoundation 0x330125df CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION + 15

3 CoreFoundation 0x33012291 __CFRunLoopDoTimer + 273

4 CoreFoundation 0x33010f01 __CFRunLoopRun + 1233

5 CoreFoundation 0x32f83ebd CFRunLoopRunSpecific + 357

6 CoreFoundation 0x32f83d49 CFRunLoopRunInMode + 105

7 GraphicsServices 0x36b452eb GSEventRunModal + 75

8 UIKit 0x34e99301 UIApplicationMain + 1121

9 AutoScene 0x000031b7 main (main.m:7)

My timer is:

[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(resetAdTimer) userInfo:nil repeats:YES];

I dont know if it's the same problem, but I've experienced times where I return to the app from the background and it just hangs.. It seems to hang for 30 seconds too which leads me to believe it's the timer code.

Is this a poor way of managing the ad fetching? Does the timer code screw up when it goes into the background for a long time?

Thanks for your help!

like image 625
Brian Avatar asked Oct 21 '22 17:10

Brian


1 Answers

You should invalidate the timer in appWillResignActive and create it again in appWillBecome active. From the docs...

- (void)applicationWillResignActive:(UIApplication *)application 

You should use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. An application in the inactive state should do minimal work while it waits to transition to either the active or background state.

like image 138
danh Avatar answered Oct 25 '22 20:10

danh