Reputation: 55885
I am wondering if it is an expensive operation to access data (string, bool) stored in NSUserDefaults
. Is that significantly slower than accessing it once and storing it in a property, then using it in the future by obtaining it from the property as opposed to getting it from NSUserDefaults
again? Or are the differences quite insignificant? I know the docs state NSUserDefaults
is cached.
The answer likely depends on how often you plan to fetch it. Let's assume it will be needed 20 times every minute. I won't be changing it programmatically, only fetching the current value.
I'm curious because one benefit of always fetching it is you can guarantee that is the current value. If you get it once and store it you need to listen for changes to NSUserDefaults
then update the value stored by fetching it.
Upvotes: 0
Views: 1100
Reputation: 3590
At runtime, you use an NSUserDefaults object to read the defaults that your application uses from a user’s defaults database. NSUserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value. The synchronize method, which is automatically invoked at periodic intervals, keeps the in-memory cache in sync with a user’s defaults database.
Following the documentation, while you aren't using the synchronize
method, fetching values from NSUserDefault
shouldn't be an expensive operation. So, while you are only fetching a value and not setting it, your operation won't be more expensive than direct access from a property.
Upvotes: 6