Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the UserDefaults register method

First of all, let me say that this is NOT a complaint, I just want to understand how does the register(defaults: []) method works.

I'm using the UserDefaults register(defaults: []) method inside the didFinishLaunchingWithOptions method to register the default value of all of my UseDefault keys, everything is working fine as expected.

My question is, why is that the values in the register(defaults: []) method do not reset everytime the didFinishLaunchingWithOptions method is called?

I do not want them to rest I'm just trying to understand why.

I have the following code...

func application(...didFinishLaunchingWithOptions...) -> Bool {

    UserDefaults.standard.register(defaults: [
        keyUserName:"",
        keyHasCar:false
        ])
}

Here is why my confusion, when the app runs, it saves the default values as expected, then, if the values get modified later somewhere in the app and right-after the app is killed and relaunched, the didFinishLaunchingWithOptions is called again but the values do not get reset, but if I add a new key to the array it does get saved but the rest of the keys do not get reset only the new one is added with its default value.

Again, I do not want the values to reset, I just need to understand how does register method works.

like image 929
fs_tigre Avatar asked Dec 08 '18 14:12

fs_tigre


People also ask

How data is stored in UserDefaults?

Updating the Prospects initializer so that it loads its data from UserDefaults where possible. Adding a save() method to the same class, writing the current data to UserDefaults . Calling save() when adding a prospect or toggling its isContacted property.

How much data can you store in UserDefaults?

Currently, there is only a size limit for data stored to local user defaults on tvOS, which posts a warning notification when user defaults storage reaches 512kB in size, and terminates apps when user defaults storage reaches 1MB in size. For ubiquitous defaults, the limit depends on the logged in iCloud user.

What do you use UserDefaults for?

At runtime, you use UserDefaults objects to read the defaults that your app uses from a user's defaults database. UserDefaults caches the information to avoid having to open the user's defaults database each time you need a default value.

What can be stored in UserDefaults?

You can use UserDefaults to store any basic data type for as long as the app is installed. You can write basic types such as Bool , Float , Double , Int , String , or URL , but you can also write more complex types such as arrays, dictionaries and Date – and even Data values.


1 Answers

From the documentation for register(defaults:):

If there is no registration domain, one is created using the specified dictionary, and NSRegistrationDomain is added to the end of the search list.

The contents of the registration domain are not written to disk; you need to call this method each time your application starts.

What this means is that the registered defaults act as a fallback to the normal, user defaults that you are working with. The registered defaults do not overwrite or replace the standard user defaults.

When you attempt to lookup a key from UserDefaults.standard, if the key isn't there, then it is looked up in the registered defaults and that result (if any) is returned.

Once you set a value in UserDefaults.standard, of course it is that value that is returned for the key. If you remove the key from UserDefaults.standard then the registered default is used again.

like image 113
rmaddy Avatar answered Sep 19 '22 10:09

rmaddy