Re: Confusion with app settings / NSUserDefaults
Re: Confusion with app settings / NSUserDefaults
- Subject: Re: Confusion with app settings / NSUserDefaults
- From: Saagar Jha via Cocoa-dev <email@hidden>
- Date: Sun, 06 Aug 2023 03:57:06 -0700
The plist file on disk is written to sporadically. cfprefsd is generally the
arbiter what the real values are, and it may or may not write things to disk
immediately. Conversely, it’s not necessarily sitting around watching the plist
file for changes. The correct way to update user defaults in an ad-hoc fashion
is to use the defaults command.
> On Aug 6, 2023, at 01:53, Gabriel Zachmann via Cocoa-dev
> <email@hidden> wrote:
>
> I am pretty confused with the NSUserDefaults system.
>
> My app stores different settings for different monitors in the
> [NSUserDefaults standardUserDefaults].
> To do so, I generate
> defaults_ = [NSUserDefaults standardUserDefaults];
>
> Then, I create a dictionary containing the default settings and register
> them, like this:
>
> NSDictionary * monitor_defaults = [NSDictionary
> dictionaryWithObjectsAndKeys: defaultsForMonitor, displayname, nil ];
> [defaults_ registerDefaults: monitor_defaults];
> NSDictionary * monitor_user_prefs = [defaults_ dictionaryForKey:
> displayname];
>
> Then, I read the actual values from monitor_user_prefs.
>
> When the user changes a setting, I do the inverse, like this:
>
> .. create a new dictionary monitor_user_prefs, containing all the
> key/value pairs of the settings ..
> [defaults_ setObject: monitor_user_prefs forKey: displayName_];
> bool success = [defaults_ synchronize];
>
> Now, the focusing part is the following.
> I delete the Preferences folder in the app's container.
> I can check with the the 'defaults' command line tool that it's really gone:
>
> % defaults read de.zach.ArtSaverApp
> Domain de.zach.ArtSaverApp does not exist
>
> I run the app, and its settings are properly populated with the defaults I
> set programmatically in the app.
> E.g., DurationPerImage is 60 (sec).
>
> *However*, when I open the plist file in Xcode, I see four key/dictionary
> pairs!
> The keys are the names of some of the monitors I have used in the past, so
> they are not entirely bogus.
> But right now, my laptop is not connected to any external monitor, just the
> internals LCD display.
> So there should be really only one dictionary, with the name "Built-in Retina
> Display".
>
> Next thing that's also very confusing is the following.
> I close the app. Then I change a value in the plist using XCode.
> Then I save the plist (CMD-S) in Xcode.
> Then I run my app again - but the setting has not changed!
>
> It seems as if macOS is keeping the settings somewhere else, instead of the
> plist file.
> OTOH, when I change a setting in the app, the value in plist file reflects
> that, even while I have the plist file open in Xcode.
>
> Very confused,
> Gabriel.
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please 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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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