TomSawyer TomSawyer - 6 months ago 26
iOS Question

iOS: Speed of reading NSUserDefaults vs variables

I have an app and it calls to variables ofently.
And these variables are stored in

NSUserDefaults
.

Im wondering where
NSUserDefaults
is storing?
And if i call to
NSUserDefaults
directly instead of using to variables.

Which is faster? variables or
NSUserDefaults
. Because using variables to store
NSUserDefaults
will be the cause of using more memory.

Answer

NSUserDefaults persists its data on disk so at some point it must load that data from disk in order to store it in memory. It will need to write it back to disk when you tell it to synchronize.

Once in memory, it will store it in a dictionary-like container (probably NSMutableDictionary).

Reading from both disk is very expensive compared to reading a variable directly and reading from a dictionary is moderately expensive compared to reading a variable.

Reading/writing variables it much quicker by a long way.