Question

I'm using an xcdatamodel to define a number of classes based upon CoreData data entities. This is working great and I can retrieve them in accordance to Apple's examples:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html

What I'm after however, is a way to package the fetch method up into another class, but I have a couple of questions... e.g.

MyDataAccessClass *mdac = [[MyDataAccessClass alloc] init]; myFetchedData = [mdac fetchData];

Q1. If I do this, is it ok that the NSManagedObjectContext is defined in the class? or does it still need to be referenced in my view controller and passed to my 'MyDataAccessClass'? Q2. It makes sense to me to have the data retrieval methods set up in the classes created by XCode for the entities in the data model. Although every time I try to do this, then update those classes automatically because they are automatically generated by XCode, they overwrite any methods I've defined.

Thanks in advance!

Was it helpful?

Solution

You might be able to create a new NSManagedObjectContext in the seperate class - not sure if there will be any issues with that since one is already created in the appDelegate. What I did was what you suggest in the second part of Q1, where I pass the NSManageObjectContext into the seperate method class so then I can do something like:

myFetchedData = [mdac fetchData:currentNSManagedObjectContext];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top