Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData countForFetchRequest says 'entity not found'

I'm experiencing a strange problem when trying to count the entities in a managed object context.

- (NSUInteger)countEntity:(NSString *)entityName 
                inContext:(NSManagedObjectContext *)context{

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName                     
                                                  inManagedObjectContext:context];
    [request setEntity:entity];
    [request setIncludesSubentities:NO];
    NSError *error = nil;
    NSUInteger count = [context countForFetchRequest:request error:&error];
    [request release];
    return count;
}

The line:

NSUInteger count = [context countForFetchRequest:request error:&error];

throws a NSInternalInconsistencyException reason: 'entity not found'

Changing to:

NSUInteger count = [[context executeFetchRequest:request error:&error] count];

works without any problem.

I'm at loss here. Any ideas?

Thanks!

/Oskar

like image 924
Oskar Avatar asked Feb 18 '26 08:02

Oskar


1 Answers

Running into this today. Started happening when I introduced a second persistent store. Do you have more than one store in the moc? -Ken

like image 62
Ken Aspeslagh Avatar answered Feb 20 '26 22:02

Ken Aspeslagh