I'm working on an app that logs a user in and downloads some data such as friends, some locations, etc. This is done in a custom Tab Bar Controller that is presented after the login screen and the data is stored in arrays. Any other View Controller has a reference to that Tab Bar Controller to grab their own copy of the data.
I also have an event listener running in the App Delegate to update some user data but I need to access the user data I've already fetched when handling the event listener. This is fine if the app is running as the Tab Bar Controller is already loaded and I can post a notification in the App Delegate and handle it in the Tab Bar Controller, but when the app isn't running then there obviously isn't an instance to handle the notification.
What would be the best way to store the downloaded data for use when the app is closed but so that the event handler can access it? I thought of Core Data but it gets updated regularly so that seems a bit overkill, but then I've never done this before so that may be the answer..
Any thoughts or lessons from experience would be great!
There are a few options for storing data. One simple approach is to store the data directly on disk in a plist. Add the data to a dictionary or array and save it using the following code:
//Read from file:
let documents = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let path = documents.stringByAppendingPathComponent("SavedFile")
if let yourData = NSDictionary(contentsOfFile: path) as? [String: AnyObject] { //Cast to correct type
//Do stuff with the data
}
//Write to disk:
let yourData = [String: AnyObject]() //Your data
let documents = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let path = documents.stringByAppendingPathComponent("FileToSave")
(yourData as NSDictionary).writeToFile(path, atomically: true)
Using plists works great for small data amounts and typically when you have some custom data to store on disk. In your case this might be sufficient.
If you have more data then Core Data is a really good option. Some of the benefits of Core Data is that it manages a lot of the mess behind the scenes, and integrates with UITableViewController very good. I will not give a full introduction to Core Data here, but in short, it is a bit more work to set up and you get the most benefits when you have multiple sets of data in the same format (for instance a "news" object containing a title, subtitle, main text, image and so on, repeated many times). Using Core Data for your storing of one user's data is probably not optimal, but it is hard to say without knowing exactly what data you are storing.
Another option is to store the data in NSUserDefaults, but this is meant mostly for settings and very small data, not list of user's friends etc (but it is a possibility).
Without knowing your exact data structure it is hard to say something for sure, but based on the description I would go with saving a plist on disk as shown in the code above, since you already have the data in Arrays.