Reputation: 2980
I have this code in my app -
- (void)applicationDidEnterBackground:(UIApplication *)application
{
...
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
if (bgTask != UIBackgroundTaskInvalid)
{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
}];
//NO MORE CODE AFTER THAT
}
I just want to extend my app's duration of running in background to handle some events from an external source. By using this code the app, sometimes the app is crashing with the logs -
[app-name] has active assertions beyond permitted time
can anyone help me with this?
Even though its crashing, it wont affect the user. The user wont get to know about the crash since the app is in background. I'm just worried about rejection by the app store review. Need urgent help! :(
EDIT : My app is communication to an external device via asynchronous TCP socket. With above code when I send my app to the background it recieves data for an additional 10 mins. After 10 minutes when I bring the app to the foreground the app hasn't closed yet, but it shows that the socket connection has disconnected. So as soon as the app comes to foreground AFTER 10 mins, it starts re-establishing the connection.
This happens in about 80% of the test cases. Remaining 20% result in the aforementioned crash.
Upvotes: 1
Views: 208
Reputation: 49376
What sort of work are you doing on the main thread whilst in the background? If you're blocking it, the expiration handler block won't be called, viz:
A handler to be called shortly before the application’s remaining background time reaches 0. You should use this handler to clean up and mark the end of the background task. Failure to end the task explicitly will result in the termination of the application. The handler is called synchronously on the main thread, thus blocking the application’s suspension momentarily while the application is notified.
This will cause the watchdog to maul you as having failed to terminate your background tasks in time.
Upvotes: 1