Rakesh
Rakesh

Reputation: 3399

Why would I need to synchronize NSUserDefaults if I haven't made changes?

The documentation for -[NSUserDefaults synchronize:] reads:

Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.

I want to know when the part in bold might happen? I mean if there are no changes made to the user defaults, then will it be reflecting the values on disk right? Why would I have to call synchronize to update the live object? Am I missing something obvious?

Upvotes: 2

Views: 211

Answers (3)

Catfish_Man
Catfish_Man

Reputation: 41821

On iOS, this can happen if the user updates something in the Settings app.

On OSX, this used to happen if your preferences were shared with another app, or if system-wide settings (say, the current language) changed. However, NSUserDefaults has been updated since then to handle this automatically, and re-read data if something has changed externally.

Note that for the latter case, the documentation is out of date. I've already filed a bug report about the docs.

Upvotes: 3

Amin Negm-Awad
Amin Negm-Awad

Reputation: 16660

User defaults can (could) be shared between apps. There is a global domain.

Upvotes: 1

Florian Mielke
Florian Mielke

Reputation: 3360

Imagine that while you are using a shared defaults object via [NSUserDefaults standardUserDefaults], the plist file on disk gets changed due to whatever reason. Your defaults object will not recognize this change unless you are calling synchronize to update your shared object.

Upvotes: 3

Related Questions