In regards to XCode templates with CoreData enabled, I’ve read unclear use of @property in window app using core data which goes over the ‘what’ in the templates. But I am having an issue with the ‘why’. By declaring the category in the implementation file, the CoreData accessors act like private methods. The problem with that is whenever you want to use CoreData elsewhere in your app, you need some extra code.
I’ve figured you need to either supply your own method that exposes the managed object context, such as…
- (NSManagedObjectContext *)getManagedObjectContext
{
return self.managedObjectContext;
}
…which will allow other parts of your app to use it.
Or you would need to jam pack your app delegate with specific methods to return managed objects, ie getProducts or setUser.
Can anyone shed light on the reasoning here?
The reason for this is because you should be using dependency injection in your designs. This is the recommended design by the Core Data team. What is expected is that your app delegate will set the
NSManagedObjectContextreference in your root view controller(s). From there the controllers will set or inject the necessary dependencies in the following view controllers.This will lead to a more flexible design. I discussed it in depth in my article on the MDN (http://www.mac-developer-network.com/articles/cd0004.html).