vilelam
vilelam

Reputation: 804

Saving some data from applicationDidEnterBackground method

I would like to save some data from an array into a plist file when the applicationDidEnterBackground is called. I'm trying to figure it out how to access my array from the applicationDidEnterBackground method. Is there any best practice to do this? Many thanks Marcos

Upvotes: 0

Views: 758

Answers (1)

rmaddy
rmaddy

Reputation: 318774

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.

Upvotes: 1

Related Questions