Hi!
I'm working on a new application that uses Core Data, and I really like what I've seen so far. An issue that's come up, however, is that I seem to be sprinkling a lot of fetch requests and new entity insertions around my code. It seems to me that it would be cleaner to hide a lot of this Core Data work behind some abstractions, but I'm having some trouble coming up with ones that seem appropriate.
One of my issues, in particular, is that I seem to be writing code like this a lot:
NSError *fetchError = nil; NSArray *fetchedLists; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
@try { NSEntityDescription *projectListEntity = [NSEntityDescription entityForName:@"ProjectList" inManagedObjectContext:[self managedObjectContext]]; [fetchRequest setEntity:projectListEntity]; fetchedLists = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&fetchError]; } @finally { [fetchRequest release]; }
This strikes me as a pretty verbose segment of code to use every time I'd like to fetch all of the ProjectList entities from a managed object context. My first thought would be that something like this deserves to be refactored out into a class method on a custom NSManagedObject subclass for the ProjectList entity, something like + (NSArray *)executeFetchRequest:(NSFetchRequest *) inManagedObjectContext:(NSManagedObjectContext *), but that feels wrong to me, too. (Plus, there'd be a bunch of duplicated code, and I'd have to make custom subclasses for most of my entities.)
Any advice or discussion would be greatly appreciated. Core Data seems well-designed and internally consistent, and I hope that figuring out the answers to abstraction questions like these will help me to understand the Core Data framework as a whole.
Thanks, Evan
|