开发者

Can I put NSFetchedResultsController inside a Managed Object Class?

I'm sick and tired of constantly having to putting/repeat the NSFetchedResultsController code for my project in virtually every file where I'm working with the Managed Object Context.

I want to reduce the amount of repetitive code. I want to put this kind of repetitive CRUD-like code inside the model class.

What I want instead is put all my custom NSFetchs inside the Managed Object Class for the relevant Entity (ie: Company.m, Employee.m).

Even the Core Books sample code from Apple does not put this code into the Managed Object Class and I'm wondering if its possible?

I tried pasting the code into my Company.m class but it keeps complaining about the managedObjectContext and also it complains that fetchedResultsController is not declared, even though its a parameter?

Ideally, I would like to put lots of different kinds of fetch request/results controller stuff inside the Entity Managed Object Class too.

But before I get ahead of myself, Is it possible, therefore, just to put all the NSFetchedResultsController stuff inside the Entity Managed Object class?

If there is a sample tutorial or project or source code that covers this, that'd be great too.

Thanks.

(code sample follows).

/**
 Returns the fetched results controller. Creates and configures the controller if necessary.
 */
- (NSFetchedResultsController *)fetchedResultsController
{

    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }

    // Create and configure a fetch request with the Book entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    // Create the sort descriptors array.
    NSSortDescriptor *authorDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    N开发者_如何学JAVASArray *sortDescriptors = [[NSArray alloc] initWithObjects:authorDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // Create and initialize the fetch results controller.
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"author" cacheName:@"Root"];
    self.fetchedResultsController = aFetchedResultsController;
    fetchedResultsController.delegate = self;

    // Memory management.
    [aFetchedResultsController release];
    [fetchRequest release];
    [authorDescriptor release];
    [sortDescriptors release];

    return fetchedResultsController;
}    


I recommend using ActiveRecord with xmod. CoreData will overwrite your CRUD if you modify your core data model. ActiveRecord makes it as easy as calling [MyManagedObject createEntity]; and NSArray *myObjects = [MyManagedObject findAll]; There is also options to pass predicates to filter the findAll call. The xmod addition generates a subclass to the generated classes so that you can add custom logic to your entities so that they do not get overridden.

Edit: I would like to add the link to this Active Record implementation since this is the one I actually use.

Edit2: This has now been renamed to Magical Record.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜