我有一些NSOperations
在我的應用程序中定期啓動。即使應用程序置於後臺,它們也應該完成。爲此,我使用beginBackgroundTaskWithExpirationHandler
方法。我可以使用beginBackgroundTaskWithName進行正常的應用程序處理嗎
我是否應該在每次啓動我的任務時使用beginBackgroundTaskWithExpirationHandler
/endBackgroundTask:
即使應用程序不會背景?或者我只是在我檢測到UIApplicationDidEnterBackgroundNotification
時纔將這個電話稱爲開始/結束方法?
方法1:使用後臺任務每次
/**
* This method is called regularly from a NSTimer
*/
- (void)processData
{
__block UIBackgroundTaskIdentifier operationBackgroundId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:operationBackgroundId];
operationBackgroundId = UIBackgroundTaskInvalid;
}];
NSOperation *operation = ...
[self.queue addOperation:operation];
operation.completionBlock = ^{
[[UIApplication sharedApplication] endBackgroundTask:operationBackgroundId];
operationBackgroundId = UIBackgroundTaskInvalid;
};
}
選項2:使用後臺任務只有當應用程序即將赴背景
/**
* This method is called regularly from a NSTimer
*/
- (void)processData
{
NSOperation *operation = ...
[self.queue addOperation:operation];
}
- (void)applicationDidEnterBackground:(NSNotification *)notification
{
__block UIBackgroundTaskIdentifier operationBackgroundId = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"EnterBackgroundFlushTask" expirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:operationBackgroundId];
operationBackgroundId = UIBackgroundTaskInvalid;
}];
// wait for all operations to complete and then
// let UIApplication know that we are done
[[UIApplication sharedApplication] endBackgroundTask:operationBackgroundId];
}
但是蘋果文檔說:「你應該調用這個方法,有時候任務未完成可能會對你的應用程序的用戶體驗造成不利影響,你可以在你的應用程序執行的任何時候調用這個方法」https:// developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler – Jan
選項2的問題在於我必須跟蹤已發生的事情開始,什麼不是。示例代碼很簡單,但真正的代碼更復雜 – Jan
實際上,在您發佈的同一鏈接中,Apple會說:「您不需要等到應用程序移動到背景以指定後臺任務。更有用的設計是在開始任務之前調用beginBackgroundTaskWithName:expirationHandler:或beginBackgroundTaskWithExpirationHandler:方法,並在完成後立即調用endBackgroundTask:方法,甚至可以在前臺執行應用程序時遵循此模式。「 – Jan