无法确定应用程序终止时会发生什么。applicationWillTerminate进入后台
纵观苹果文档的applicationWillTerminate它说:
对于不支持后台执行或者是针对iOS的3.x或更早,当用户退出应用这种方法总是被调用关联的应用。对于支持后台执行的应用程序,当用户退出应用程序时,通常不会调用此方法,因为在此情况下应用程序只是移至后台。但是,此方法可能会在应用程序在后台运行(未挂起)并且系统因某种原因需要终止它的情况下调用。
支持后台执行的应用程序意味着什么?这是否意味着“背景”必须在应用程序的设置中启用,还是指的是旧版本的iOS?
为什么应用程序进入背景而不是终止?它会保持多久后台?
我想在我的解析后台更新的对象的时候,应用程序,我用这个进入的背景:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
//End the Task
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if([self getController]){
CatsViewController *catsViewController = [self getController];
if(catsViewController.currentUser){
int count = (int)[MyViewController.currentUser.messages count];
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
currentInstallation.badge = count;
[currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
}
else{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
}
else{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
}
从现在看来,这文件是不是100%,这将被调用时,一个应用程序被刷到终止。如果是这种情况,那我该如何保存我的数据?
背景的意思。 iOS已经引入了一些可以在后台工作的功能,如“获取设备位置”等。 –
因此,这并不意味着该应用必须为特定应用启用后台应用刷新功能? – Kex
@Kex no;这只是一种特殊的背景执行。但你需要使用其中之一。常见问题:你实际上想要达到什么目标?在任何情况下,没有收到'applicationWillTerminate'都不应该是个问题。 – Tommy