Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alansvits/add1fe47b6b92518967e33d1e3da95d4 to your computer and use it in GitHub Desktop.
Save alansvits/add1fe47b6b92518967e33d1e3da95d4 to your computer and use it in GitHub Desktop.
Saving some data from applicationDidEnterBackground method
Put the code in the class that actually has the data. Have the class register for the UIApplicationDidEnterBackgroundNotification notification.
// Put this in the `init` method
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backgrounding) name:UIApplicationDidEnterBackgroundNotification object:nil];
// The method that gets called
- (void)backgrounding {
// save the data
}
// Put this in the `dealloc` method
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
With this setup you don't have to get anything into the UIApplicationDelegate and the responsibility is kept where it belongs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment