Jonathan
Jonathan

Reputation: 1508

Passing NS Core Data context ref between View Controllers

In my initial View Controller, I set up my NS Core Data document, and get the managedOBjectContext. I then pass this to whichever view controller that comes next. It works, but it seems ugly. Any time I have a new segue or move to another area of the application, I need to pass this reference.

Wouldn't it be easier to create this reference in the appDelagate and instead have this globally available to all view controllers?

Upvotes: 2

Views: 1189

Answers (2)

johan
johan

Reputation: 6666

You don't have to pass the core data context. It's possible to retrieve it in the view controller by doing something like this:

NSManagedObjectContext *context = [(MyAppDelegate *)[[UIApplication sharedApplication\ delegate] managedObjectContext];

However I actually prefer doing it by passing the context when initiating a controller.

Upvotes: 1

Phillip Mills
Phillip Mills

Reputation: 31016

Here's an article on a good method of making a data manager that exists outside your controller hierarchy and can be used where needed: http://nachbaur.com/blog/smarter-core-data

The write-up explains the theory and has links to an implementation.

Upvotes: 3

Related Questions