I wonder what the guidelines are for:
1 – how often I can read from NSUserDefaults
2 – how much data I can reasonably store in NSUserDefaults
Obviously, there are limits to how much NSUserDefaults can be used but I have trouble determining what’s reasonable and what isn’t.
Some examples among others:
-
If my game has an option for the computer to be one of the players, I will use NSUserDefaults to save that boolean value. That much is clear. But is it also reasonable to access NSUserDefaults during my game every time I want to know whether the computer is a player or should I be using an instance variable for that instead? Assume here I need to check that boolean every second. Is the answer the same is it’s 100 ms instead? What about every 10 s?
-
If my game has 50 moving objects and I want their positions and speeds to be stored when the user quits the app, is NSUserDefaults a reasonable place to store that data? What about 20 moving objects? What about 200?
Don’t worry about limits. Instead, ask yourself this simple question:
Is this a preference?
If it is a preference, then it should be in user defaults. That’s what user defaults is for. If not, then it should be in the Documents directory (or, on the Mac, possibly in Application Support).
On iOS, you might tell whether it’s a preference or not by whether it would be appropriate to (if possible) put it in your settings bundle for display and editing in the Settings application. On Mac OS X, you can usually tell whether it’s a preference or not by whether it would be appropriate to put it in the Preferences window.
Of course, that relies on your judgment. Stanza for Mac, for example, gets it wrong, putting non-preferences in its Preferences window.
You can also consider the question by its converse:
Is this user-created data?
A preference that you will have a default value for is not user-created data; it is user-overridden data. No less bad to lose it, but it informs where you should keep it.