0

My iOS app doesn't run in background. When home button is hit the app will terminate.

Below is my code

- (void)applicationDidEnterBackground:(UIApplication *)application 
{
  double sleepTime = [[UIApplication sharedApplication] backgroundTimeRemaining] - 1;

  [self performTaskforTime:sleepTime];
}

If the application is relaunched in no time, the New instance of the app will killed with the error :

Exception Type: 00000020 Exception Codes: 0x8badf00d Highlighted Thread: 0

Application Specific Information: My_App failed to launch in time

I can't reduce the sleep time. Is there any solution for this?

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
nswamy
  • 1,041
  • 9
  • 16

2 Answers2

0

You need to return from - (void)applicationDidEnterBackground:(UIApplication *)application, do something like this:

- (void)applicationDidEnterBackground:(UIApplication *)application 
{
  UIBackgroundTaskIdentifier btid = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

  double sleepTime = [[UIApplication sharedApplication] backgroundTimeRemaining] - 1;

  dispatch_async(/* background queue */, ^{
    [self performTaskforTime:sleepTime];
    [[UIApplication sharedApplication] endBackgroundTask:btid];
  }
}

You don't want to block the main queue with a long running task as you won't be able to service the UI otherwise. Your application is busy with -performTaskforTime: and thus can't launch in time.

hypercrypt
  • 15,389
  • 6
  • 48
  • 59
  • I tried this, since my app doesnt run in background will quit and the task will be never performed. – nswamy Oct 19 '11 at 18:17
  • Updated my answer. You need to call -beginBackgroundTaskWithExpirationHandler: to run in the background. – hypercrypt Oct 19 '11 at 21:02
0

You can't block the main application thread for more than a few seconds. See this SO post.

Link

Community
  • 1
  • 1
logancautrell
  • 8,762
  • 3
  • 39
  • 50
  • I see the app is launched as a new instance, and in the new instance the thread is not blocked. – nswamy Oct 19 '11 at 18:26