Question

I'm trying to understanding using fetch requests in core data. I would like to make a fetch request to select a Chord entity from core data.

@interface ChordViewController ()

@property NSFetchRequest* fetchRequest;
@property NSEntityDescription* entityDescription;
@property NSManagedObjectContext* managedObjectContext;

@end

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    self.managedObjectContext = [[NSManagedObjectContext alloc] init];
    self.fetchRequest = [[NSFetchRequest alloc] init];
    self.entityDescription = [NSEntityDescription entityForName:@"Chord" inManagedObjectContext:self.managedObjectContext];
    [self.fetchRequest setEntity:self.entityDescription];
}
return self;

Is the fetch request reusable, and if so, is it good practice to reuse the same fetch request between fetches such that is it a class member property? I would also like to know if the entityDescription should be a class member property too.

Thank you!

Was it helpful?

Solution

Fetch requests and entity descriptions can be reused, but the cost of creation is relatively low so recreating isn't much of an issue. Predicates are more costly to recreate if you use any.

I assume your code is just typed in as creating a managed object context like that won't work (it has no connection to a persistent store...).

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