I asked about this earlier. I think I phrased my question wrong. Basically I want to save data from my app to a parse backend when the application is terminated. i.e. the app is swiped and killed from the list of apps. iOS documents say that actually applicationDidEnterBackground will be called and NOT applicationWillTerminate so any work can be done in this method.
applicationWillTerminate: For apps that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the app. For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason.
However this isn't 100% and from my testing applicationDidEnterBackground isn't called every time I quit the app. So how does one save data when an app is terminated with a 100% guarantee it will actually be saved?
This is my code for saving when applicationDidEnterBackground:
- (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;
}
});
}
Would be great to get any pointers on this. thanks