Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do you put global application data in an iPhone app?

Tags:

iphone

I have an app that allows the user to view, manipulate and manage a collection of Schedule objects. Different parts of the app need access to this data. I've experimented with making the collection a singleton, but I didn't find that very clean.

I recently changed to storing the global data in the AppDelegate class and using:

MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.myGlobalData doSomething];

Which eliminates the need for my global data to be a singleton, but is really just taking advantage of the UIApplication singleton.

There really isn't just one primary view controller where it makes sense to store it in my case. So I was wondering what different strategies people use to address this.

like image 786
davidavr Avatar asked Feb 05 '09 13:02

davidavr


People also ask

Can you access app files on iOS?

On your iPhone, iPad, or iPod touch, open the Files app. Don't see the blue Files app icon on your Home Screen? Just swipe down, then search for the Files app. In the Files app, search or browse for the file that you want.

How do you declare a global variable in access?

We try to prefix all variables with a letter indicating the scope of the variable. This makes it easier to figure out where the variable has been declared. All global variables should be prefixed with "G". All local variables should be prefixed with "L".


1 Answers

If you've got a central data model that needs to be accessible to multiple controllers, there's nothing wrong with using a singleton to manage the data-- a "sharedManager" is exactly what the scenario demands. This is essentially what Apple does with their address book API for manipulating contacts. They use a C-style API but it involves getting a reference to the shared address book database and then using that in future calls.

Using the app delegate works too, but as you note it's really the same thing. You're designating the app delegate singleton as the owner of the data model, which is OK too.

NSUserDefaults can be abused in this way but that's really not its purpose. Aside from being kind of ugly, it also puts restrictions on the type of data you can store. Your own model can store whatever data you like, but NSUserDefaults is not quite so flexible.

Using SQLite or a property list or whatever to store the data to a file is really orthogonal to the question of how to manage it. From your description a singleton approach sounds reasonable, and that singleton can write to files in whatever way makes sense for the data you're working with.

like image 98
Tom Harrington Avatar answered Oct 15 '22 02:10

Tom Harrington