Saving user defaults before program ends
Saving user defaults before program ends
- Subject: Saving user defaults before program ends
- From: Denver Timothy <email@hidden>
- Date: Tue, 16 Jan 2007 17:03:41 -0700
Hi All,
I have created a preferences window and successfully made my
AppController class the delegate for the window and which implements
the windowWillClose method. The windowWillClose method calls [self
saveUserDefaults]. In saveUserDefaults, I have two NSLog calls, one
at the top and one at the bottom, and then all the [defaults
setObject:forKey] calls in between. Both NSLog() are executed as
expected.
The problem is that if the program is terminated (eg. Apple-Q),
_none_ of the defaults are saved, but the two NSLog() calls _are_
executed as expected. However, if the the window is simply closed,
the defaults are saved as expected.
Here is the relevant code:
- (void)windowWillClose:(NSNotification *)aNotification
{
[self saveUserDefaults];
}
- (void)saveUserDefaults
{
NSLog( @"Saving user defaults" );
NSUserDefaults *defaults;
NSData *deviceAsData;
defaults = [NSUserDefaults standardUserDefaults];
// Enabled
BOOL monitoring = ( [enabledButton state] == NSOnState ? TRUE :
FALSE );
[defaults setBool:monitoring forKey:@"enabled"];
...
// Device
if( device ) {
deviceAsData = [NSKeyedArchiver archivedDataWithRootObject:device];
[defaults setObject:deviceAsData forKey:@"device"];
}
[self stopMonitoring];
[self startMonitoring];
NSLog( @"Done saving defaults" );
}
Am I doing this properly? Should I be doing something different
entirely?
Thanks,
Denver Timothy
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden