2

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.

Hudson Buddy
  • 726
  • 2
  • 9
  • 20

1 Answers1

3

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 NSUserDefaults are 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:

Use a singleton class, I use them all the time for global data manager classes that need to be accessible from anywhere inside the application.

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:

Community
  • 1
  • 1
redlightbulb
  • 1,466
  • 11
  • 9
  • still reading some of this documentation, but seems like a good way to go. I will be wanting to modify this singleton class periodically though, so everything has to be mutable...but its a good start – Hudson Buddy Dec 12 '12 at 08:50
  • Glad I could help @HudsonBuddy! You can do anything you like in a Singleton class, so storing your data as NSMutableDictionaries is a fine thing to do. Good luck! – redlightbulb Dec 12 '12 at 17:29
  • Hey again, is there a way to persist this data beyond each session? As I am testing, each time I build the project the singleton class is cleared, but the NSUserDefaults isn't and it serves as a singleton class as well...Any ideas? – Hudson Buddy Dec 13 '12 at 07:02