There seems to be a lot of misunderstanding regarding user defaults. Think of it this way. This is essentially the same as the global dictionary available throughout the application. If you add / edit / delete a key / value in the global dictionary, this change will be immediately visible anywhere in your code. Since this dictionary is in memory, everything will be lost when your application terminates, if it is not saved in a file. NSUserDefaults automatically saves the dictionary to a file every time.
The only reason the synchronize method exists is because your application can tell NSUserDefaults to save the dictionary "now" rather than wait for the automatic save, which will eventually happen.
And the only reason you need to do this is because your application may be terminated (or crash) before the next automatic save.
In my applications, the only place I call synchronize is in the delegate method applicationDidEnterBackground . This is to ensure that the last unsaved changes are saved if the application is terminated in the background.
I think that most of the confusion comes from debugging the application during development. During development, it is not uncommon that you kill an application using the "stop" button in the debugger. And many times this happens until the last NSUserDefaults changes are NSUserDefaults . So Iβm used to putting my application in the background by pressing the Home button before killing the application in the debugger when I want the latest updates to be saved.
Given the above summary, let's look at your questions:
should it be called every time the user changes the application settings?
No. As described above, any changes are automatically available immediately.
Or should I just trust that the background api will handle this?
Yes, trust auto-save, except when you call synchronize when your application enters the background.
And is the view held immediately after changing the settings in memory as a result of losing this value?
It does not affect. After adding / editing / deleting the key / value in NSUserDefaults , the change is performed.
Also, when a synchronize () call may fail, will the user settings not be changed correctly?
The only time a change can be lost is if your application is terminated before the latest changes are saved. Calling synchronize when your application comes to the background solves most of these problems. The only remaining problem is if your application crashes. All unsaved changes that have not yet been saved will be lost. Correct the application so that it does not work.
Also, what is the cost (performance, memory or otherwise) of calling this method? I know that this is related to reading and writing to / from disk, but does it really work so much on phones?
Auto save is done in the background and just writes the dictionary to the plist file. It is very fast if you do not follow the recommendations. This will be slower if you use NSUserDefaults to store large amounts of data.