I am about to start a new Project for IOS and I will be using Core Data as my primary means of storing data for the end user. I have come into a problem in my planning stage of the application as to how I would go about saving, loading and deleting core data objects. Here are the three possible methods which I could think of:
Method 1
Create a universal data object in the Application Delegate that I will retrieve into each view controller using the UISharedApplication method. This would mean that when the application launches, It would create an object with Getters and Setters.
Method 2
Create a data model class (Subclass of NSObject) and initialise that in every view controller. This would mean that every view controller would have to have it’s own NSManagedObjectContext (Application delegate will provide that on launch) that it would pass to the data model class on initialisation. This means that throughout the view controller, it would use it’s own data model class and call methods from that.
Method 3
Have each individual view controller do the saving by itself. e.g. a -(IBAction)didAdd; would have all the code in it to save to the database. This would make it easier to have efficient data calls as it would be able to have a NSFetchResultsController with the correct settings based on the view controller (Batch size, etc.).
I am not sure which way is the best way for me to go and I would appreciate it if any of you could let me know which method is best, or if none are good, what is the best way to do this.
There is also a method number 4, which is similar to your method 1: put your data access code into a model class, and make it a singleton. This is the standard way to share model state among different controllers. Putting all your data management code into that singleton model helps you avoid code duplication.