Let’s say I need to save certain data, like an array, to NSUserDefaults.
If I need to access that data from different places in my app, is it more efficient to pass it around from view controller to view controller, or to pull it out of NSUserDefaults each time it’s needed?
Remember that I need to save it, so discount the overhead involved in saving the data. Basically, is there more overhead in creating and setting an ivar, or in loading data out of NSUserDefaults?
I always prefer to create a singleton object that can be accessed from anywhere in the app to hold this kind of information. Say for example you create a class called
DataStore. Create a class methodsharedDataStorelike so:In the
initmethod of your data store class you do all the setup, read in theNSUserDefaultswhat-have-you. Then add properties and/or methods for reading/writing the data you need to use and you can ensure that it’s properly cached etc for multiple reads.Then from anywhere in your app you can include the
.hfile and call:and be assured that you’re reading and writing your data centrally and efficiently because your app only creates one instance of the object.