Reputation: 8579
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
Upvotes: 5
Views: 1463
Reputation: 783
Here's the solution for this. If you swipe to quit your app it will not call the applicationDidEnterBackground because the app terminated. The AppDelegate method to get called when your app is terminated is this method:
1.) You need to add the appWillTerminate observer in you app delegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//This is the observer you need to add so that the applicationWillTerminate app delegate method will get called.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
}
2.)Add the codes for saving before your app is terminated.
-(void)applicationWillTerminate:(UIApplication *)application{
//Add your codes like saving data here
}
Upvotes: -1
Reputation: 101
You could use applicationWillResignActive for things like that. It has the added bonus of being called when the user opens his notification center, too. It will also be called when the user enters the app switcher. If he then kills the app, there is nothing you can do about it, because swiping the app from the switcher will send a SIGKILL. If you are lucky, you can do your background task before that happens by utilizing willResignActive.
I always use applicationWillResignActive and applicationDidBecomeActive respectively, because they both cover a wider range of situations where the app would be not in the foreground.
Also read this answer here (and above)
Upvotes: 5