Re: Saving user defaults before program ends
Re: Saving user defaults before program ends
- Subject: Re: Saving user defaults before program ends
- From: "I. Savant" <email@hidden>
- Date: Tue, 16 Jan 2007 19:18:51 -0500
You should give this document a read:
http://developer.apple.com/documentation/Cocoa/Conceptual/
UserDefaults/Tasks/UsingDefaults.html
"In applications in which a run-loop is present, synchronize is
automatically invoked at periodic intervals. Consequently, you might
synchronize before exiting a process, but otherwise you shouldn’t
need to."
Specifically, you can call -synchronize to write the defaults to
disk, and you probably should since your app is in the process of
quitting when -windowWillClose: is called. It might just not be
getting a chance to call -synchronize.
Though I really don't know *for sure* that this is what's
happening, a quick trip to the docs suggests you should try calling -
synchronize ...
--
I.S.
On Jan 16, 2007, at 7:03 PM, Denver Timothy wrote:
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:
40gmail.com
This email sent to email@hidden
_______________________________________________
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