1
votes

IOS newb struggling with core data. The Xcode sample project puts core data in app delegate. Then in the app delegate.m file, within the application didFinishLaunchingWithOptions: method it includes a call to the core data files: controller.managedObjectContext = self.managedObjectContext; which fires up the managedobjectcontext in the main controller.

I have read, however, that the app delegate file is not really where you should put the core data stuff but rather you should put it in a model file and call it as a singleton.

My problem is if you move it out of the app delegate file into a file with no application didFinishLaunchingWithOptions: method, how do you make sure the managed object context is available where you need it i.e. in other view controllers? In other words if all the core data stuff is in the model file, what is the correct syntax to invoke it from a controller file or alternatively makes it available to controller files from model file.

This problem has been driving me crazy for two weeks so would greatly appreciate any suggestions.

1

1 Answers

1
votes

Please refer to this example: http://www.objc.io/issue-10/networked-core-data-application.html

There is link to the source code on github, it shows one way of moving coreData code from AppDelegate to a separate model class PersistentStack. In the example, AppDelegate still maintains a reference to the managed object context indirectly through the PersistentStack instance.

There are many other ways to separate coredata code to a separate class depending on the need for modularity.