I am making a request for an array of perhaps 10-100 objects, all of which are JSON objects that I parse into NSDictionary’s. I want to cache this and use this data across the entire application. Is NSCache useful for this or is it better to use NSUserDefaults or what is actually the most accepted way of persisting data across an entire app? CoreData? I’m a iOS newb and don’t have too much experience in this.
Share
What you are looking for is a way to access data across your app. This is typically the role a Model plays in MVC.
CoreData and
NSUserDefaultsare ways to save data so it is not lost when your app closes or is quit. They can be parts of a Model, but do not help in having that data be accessible throughout your app.If you want an object that stores data and can be accessed anywhere in your code, you are probably looking for a Singleton.
As this excellent Stack Overflow answer explains:
The author provides some sample code you might find helpful.
This would allow you to create a simple object accessible throughout your program that has your NSDictionaries. Because it is a singleton, other classes in your program can easily access it – meaning they can also easily access the NSDictionaries you’ve stored in it.
If you do decide you want to save data, that singleton object would also be an ideal location to write any load and save code.
Good luck!
Other good resources are: