Dans mon application iPhone, j'ai une tâche d'arrière-plan qui démarre lorsque l'application entre dans l'état d'arrière-plan. La tâche fonctionne bien et est structurée en une boucle comme celle-ci:La tâche d'arrière-plan de l'iPhone s'arrête à un moment donné
run. dormez pendant 5 min. cours. dormez pendant 5 min.
etc.
Pour une raison quelconque, la tâche cesse de fonctionner après un certain laps de temps ... dire une demi-heure à une heure.
Quelqu'un peut-il m'aider à comprendre pourquoi? Voici le code de tâche de fond:
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"Application entered background state.");
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"bgCheckSwitch"] == YES) {
//UIApplication* app = [UIApplication sharedApplication];
// Request permission to run in the background. Provide an
// expiration handler in case the task runs long.
NSAssert(bgTask == UIBackgroundTaskInvalid, nil);
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Synchronize the cleanup call on the main thread in case
// the task actually finishes at around the same time.
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task.
[someClass doSomeThing]; //The actual method performed by the task. The looping is in the method.
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
});
}
}
Comment votre tâche d'arrière-plan peut-elle fonctionner pendant une heure? En utilisant un code similaire, ma tâche est tuée après une minute. Et essentiellement c'est juste une minuterie non répétitive. Comment obtenez-vous le "sommeil pour 5min"? -Merci – Dimitris