Reputation: 2524
I value the opinions on this site and wanted to know what you all thought. I have an app that contains an array. This array is used in several view controllers. Currently, I create the array each time one of the view controllers opens up. I don't believe this to be the best thing to do. I have used NSUserDefaults before, and singletons, but am open to any ideas you have, plist, etc. I am looking on making things run quick and efficient.
Also, I wanted the user to be able to rearrange the array. Lets say it starts out in alpha order. But the user finds it better to rearrange it some other way. I know how to do that, rearrange, but I wanted to know where to keep the original array so that the user can than reset the order back to the original alpha order.
So I guess this question is twofold. Where to store the original array, and how best to then access the changed array throughout the app. Thanks very much for your help.
EDIT #1
I want to thank everyone for their comments and answers! This is what, I feel, the community is about. Learning new and different techniques is great. I am going to attempt to try all the answers (to see if I can actually program it) and see which one works the best for me. Once again thanks for this excellent dialogue.
Edit#2
I ended up doing a bit of both, and I would have never done that without all of your comments and answers. I made the array in the app delegate and saved it in user defaults. It wasn't a basic array, it was an array of custom objects so I had to use a bit of NSCoding. It also was not just alpha sorted, so I made one array that could be manipulated. Once again, thanks for all the great insight!
Upvotes: 2
Views: 163
Reputation: 3780
You can create a singleton instance of it in your AppDelegate. Make an @property for the array in AppDelegate.h, and then import that header into your other viewControllers.
Init and store the array to the appDelegate's array @property in "didLoadWithOptions." You could create custom getters/setters to allow for manipulation. The actual data could come from an external XML, JSON or plist file, but to test you can just hard-code an array in AppDelegate to start with.
Each viewController needs to have a reference to the appDelegate, using the methods described in this answer. You can then access the 1 array as you would any other property,
NSArray *dataInVC = myAppDelegate.singletonArray;
(...or using NSMutableArray if you want to add/change/manipulate).
...Alternatively, just have a separate class for your data, and create a singleton version of it so you can more easily refactor later if necessary. Examples of how to do this in this answer
Upvotes: 2
Reputation: 4168
As you are accessing your data from five different places, it will soon become confusing to send data to and fro. A singleton class with public properties storing your arrays would be a good solution to this problem.
In case you want to persist the data to disk, NSUserDefaults
would be the better option.
Here's how one would go about using NSUserDefaults
to do that. For saving data:
[[NSUserDefaults standardUserDefaults] setObject:originalArray forKey:@"OriginalArray"];
[[NSUserDefaults standardUserDefaults] setObject:modifiedArray forKey:@"ModifiedArray"];
Retrieving data:
NSMutableArray *modifiableArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"ModifiedArray"].mutableCopy;
Don't forget to save the data to disk with a call to [NSUserDefaults synchronize]
at appropriate times.
Upvotes: 1
Reputation: 7410
I'd recommend storing the array in your app delegate. One of the advantages of this solution is that your app delegate is already a singleton.
Create an NSMutableArray
property in your app delegate header file. Init and fill the array in the app delegate didFinishLaunchingWithOptions:
method.
Access it from anywhere with the following code :
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
// appDelegate.myArray holds your values
Going even further, add this to your Prefix.pch file:
#import "MyAppDelegate.h"
#define appDelegate ((MyAppDelegate *)[UIApplication sharedApplication].delegate)
Then the previous code can be reduced simply to calling appDelegate.myArray
from anywhere.
You can create methods that handle sorting and other manipulation on your array in your app delegate implementation. If you declare those methods in the app delegate header file, you can also call them from anywhere in the app with :
[appDelegate doStuff:parameter];
Upvotes: 1