CoreData countForFetchRequest says "object not found"

I am having a strange problem when trying to count objects in the context of a managed object.

- (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;
}

Line:

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

throws out a NSInternalInconsistencyException reason: 'entity not found'

Go to:

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

works without problems.

I'm at a loss here. Any ideas?

Thanks!

/ Oskar

+3
source share
6 answers

Running in today. It started when I introduced the second permanent store. Do you have more than one store in your store? Ken

+4
source

For what it's worth, I just ran into a similar mistake where executeFetchRequest:errorand countForFetchRequest:error:do not agree with the number of objects in a sample query.

: NSManagedObjectContexts NSPrivateQueueConcurrencyType ( ), , NSMainQueueConcurrencyType ( gui). (, MagicalRecord)

ViewController, NSManagedObjectContextDidSaveNotification . , countForFetchRequest:error: , , . executeFetchRequest:error .

wierd.

+1

:

  • error ? , , , , - .
  • [request entity] ? , - , : , , reset , . , NSFetchRequest .
  • , , ? ? .
0

, entity nil (.. context , , - ). , entity nil +[NSEntityDescription entityForName:inManagedObjectContext:].

0

countEntity: , . ( "_countWithNoChangesForRequest: : ".), ( Widgets). , .

0

The problem you are experiencing is due to improper use of countForFetchRequest: error :. In fact, in the code snippet, you first execute a fetch request using executeFetchRequest: error, then move on to using countForFetchRequest: error :.

From the method documentation:

Returns the number of objects that the given fetch request would return if it were passed to executeFetchRequest: error :.

Therefore, you should not execute a fetch request before calling countForFetchRequest: error :.

-1
source

Source: https://habr.com/ru/post/1720854/


All Articles