2010-09-16 2 views
2

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; 

      } 

     }); 

    }); 


} 

} 
+0

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

Répondre

2

tâche de fond ne fonctionne que pendant une période de temps, le système d'exploitation le tue. Ceci est discuté dans les vidéos multitâche WWDC10.

+0

Je pense que vous avez un total de 5 minutes de temps de traitement pour terminer une tâche. Cela peut être augmenté dans les révisions ultérieures de IOS – Petesh

+0

J'oublie exactement, mais je pense que c'est 10 minutes. Je sais que les vidéos vous le disent. – jer

+0

Cela pourrait être pratique: http://stackoverflow.com/questions/3388279/iphone-task-completion – Petesh

Questions connexes