Re: Forcing NSUserDefaults being written to disk?
Re: Forcing NSUserDefaults being written to disk?
- Subject: Re: Forcing NSUserDefaults being written to disk?
- From: Mike Ferris <email@hidden>
- Date: Thu, 9 Jan 2003 10:47:31 -0800
When you call -registerDefaults the values go into the "registration"
domain which is not a persistent domain. In NSUserDefaults there's a
stack of domains (which used to be simple and is not actually quite
complicated with the introduction of things like per-host and any-user
defaults when CFPreferences came along...) Domains are either
persistent or not. The canonical examples of non-persistent domains
are the registration domain and the command-line-arguments domain (any
command line argument pair "-<foo> <bar>" will cause the default <foo>
to be set to <bar> in the command-line-argument domain.)
If you want to set defaults that will stick, the usual way is simply to
send -setObject:forKey: to NSUserDefaults. This convenience method
(and the others like it) will put the value into the most common domain
(the this-user, this-app, any-machine domain).
Otherwise you need to use the API for fetching and setting whole domain
dictionaries if you need the value to go into some other domain.
Note that it is often customary not to write out preferences until the
user actually changes them from their default values but this is
something that an app either does or does not do. It is not a feature
of NSUserDefaults. With NSUserDefaults if you put a value in a
persistent domain, it will be written.
Mike
Begin forwarded message:
From: Pierre-Olivier Latour <email@hidden>
Date: Wed Jan 8, 2003 12:51:00 PM US/Pacific
To: Cocoa Developers <email@hidden>
Subject: Forcing NSUserDefaults being written to disk?
Hi,
I have an application that has no UI. Its settings have to be set by
editing
its defaults file (.plist). I use the following code to register the
defaults at startup:
+ (void) initialize
{
NSMutableDictionary* defaults = [NSMutableDictionary
dictionary];
[defaults setInt:640 forKey:kKey_ScreenWidth];
[defaults setInt:480 forKey:kKey_ScreenHeight];
[defaults setInt:32 forKey:kKey_ScreenDepth];
[defaults setInt:16 forKey:kKey_ZDepth];
[defaults setBool:YES forKey:kKey_VerticalSync];
[defaults setInt:0 forKey:kKey_MaxFPS];
[defaults setBool:YES forKey:kKey_LogFile];
[defaults setInt:8765 forKey:kKey_ServerPort];
[defaults setBool:NO forKey:kKey_HighPriorityThread];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}
My problem is that, even if I call [[NSUserDefaults
standardUserDefaults]
synchronize] when application quits (it returns true), no file is
written in
~/Library/Preferences/;
According to the doc, it should be written when you call synchronize,
even
if no changes have been made to the defaults...
So I tried changing artificially the defaults before calling
sycnhronize
i.e:
[defaults setBool:![defaults boolForKey:kKey_VerticalSync]
forKey:kKey_VerticalSync];
[defaults setBool:![defaults boolForKey:kKey_VerticalSync]
forKey:kKey_VerticalSync];
Now a file is created but it contains only the key kKey_VerticalSync!
Any idea?
PS: I assume synchronize if buggy.
_____________________________________________________________
Pierre-Olivier Latour email@hidden
Palo Alto, USA http://www.pol-online.net
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.