When and why should you use the sync () method of NSUserDefaults?

So, I looked at the apple documentation using the sync () method of NSUserDefaults. See below for reference:

https://developer.apple.com/reference/foundation/userdefaults/1414005-synchronize

The page currently reads:

Since this method is automatically called at periodic intervals, use this method only if you cannot wait for automatic synchronization (for example, if your application is about to exit) or if you want to update the user's default disk settings, even if you have not made any changes.

However, what I still do not understand is when this method should be called? For example, should it be called every time a user changes application settings? Or should I just trust that the background api will handle this? And is there a deduction of the view immediately after changing the settings in memory that the loss will be lost?

Also, when a synchronize () call may fail, will the user settings not be changed correctly?

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?

+5
source share
3 answers

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.

+18
source

The Apple documentation for synchronize () has been updated and now reads:

Waits for any pending asynchronous database updates by default and returns them; this method is not needed and should not be used .

+2
source

As far as I know, synchronization is used to synchronize data immediately, but iOS can handle this in a smart way. Therefore, you do not need to call it every time. If you call it every time, it will turn into a performance problem.

Check Apple Documentation: Official Link

0
source

Source: https://habr.com/ru/post/1260362/


All Articles