Apple's documentation of "App States and Multitasking" (Section "What to Do When Moving to the Background") say for saving when the app goes to background:
Save user data and app state information. All unsaved changes should be written to disk when entering the background. This step is necessary because your app might be quietly killed while in the background for any number of reasons. You can perform this operation from a background thread as needed.
When I start a dispatched operation e.g. for saving which takes some time at applicationDidEnterBackground:
like the following I don't get the NSLog output when pressing the home button. After returning to the app, the NSLog output appears.
- (void)applicationDidEnterBackground:(UIApplication *)application {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self performOperation];
NSLog(@"Operation finished");
});
}
Can I be sure that the performOperation
method was performed completely or did it interrupt when the app entered sleep mode?
applicationDidEnterBackground:
is called. 2. Shouldn'tbgTask
be declared as a block variable and in the dispatching block after[self performOperation]
also be called[app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid;
? 3. What happens when the user taps the app again before the operation is completed or before the background task is ended? – Sirenasirenic