NSUserDefaults don't save with app groups

Hello,


I encounter the following problem with NSUserDefaults and app groups:


I added a today extension to my main app and created an app group so that I can share data with the extension. I moved my user defaults to the app container by using

NSUserDefaults(suiteName: SharedConstants.appGroup)

(where SharedConstants.appGroup is a constant from a dynamic framework). I instantiate the user defaults in the extension the same way. Note that I successfully created the app group in the developer portal and it displays as correct in both targets. I successfully share CoreData with the extension.


My main app registers default values for all settings I store in the NSUserDefaults by reading a plist file from the app's main bundle like this:

NSBundle.mainBundle().URLForResource("DefaultSettings", withExtension: "plist")

This worked with standardUserDefaults as expected and I did not change this due to the fact that the extension does not need to register default values (though there are fallbacks for the rare case a user uses the extension before using the app).


Now the problem: the extension successfully instantiates the shared user defaults but they do not contain the settings saved by the main app. I then went on to not register the defaults in my main app and it crashes very early because some default is nil. So the userdefaults somehow are not saved. The mysterious thing is, that when I let my app register the defaults and then use the app and change some settings which are saved to the user defaults, the app successfully remembered those non-default setting values which means that at least something is saved. I also played with synchronizing the user defaults to make sure that I force a persistent save. Unfortunetly, I can't read this saved stuff from my today extension as the user defaults that are instantiated with the suite name are empty despite of some standard parental restrictions values.


Does anyone know a solution to this, is this a bug or any wrong thinking on my side?

Regards

Answered by junkpile in 35163022

My understanding is that the default values are in-memory only, read only. So if your main app asks for a value, NSUserDefaults first looks in the persistent store, and if not found, consults the in-memory defaults. The extension would not see the "default defaults" because they are not written to persistent storage anywhere. If you need those values available in the extension, on your main app startup you would have to iterate through your dictionary from the plist and explicitly write the values to NSUserDefaults if not already set.

Accepted Answer

My understanding is that the default values are in-memory only, read only. So if your main app asks for a value, NSUserDefaults first looks in the persistent store, and if not found, consults the in-memory defaults. The extension would not see the "default defaults" because they are not written to persistent storage anywhere. If you need those values available in the extension, on your main app startup you would have to iterate through your dictionary from the plist and explicitly write the values to NSUserDefaults if not already set.

Thank you! My bad, I could have read that up in the documentation; just expected different behaviour from this method.

NSUserDefaults don't save with app groups
 
 
Q