I am executing a 'countForFetchRequest' and it is always returning 1 more count than the number of records in my table.
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"TESTDATA" inManagedObjectContext:self.managedObjectContext]];
NSError *error = nil;
NSUInteger count = [self.managedObjectContext countForFetchRequest:request error:&error];
if(count == NSNotFound) {
// Handle error
}
return count;
That returns 1 when my table is empty. Returns 4 if my table has 3 rows.
sqlite> select count(*) from ZTESTDATA;
0
sqlite>
Any ideas on what I am doing wrong?
Count is always accurate but it counts both what is in memory (not saved yet) and what is on disk.
As others have suggested, do a fetch instead of a count and print out what Core Data finds and you will find your answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With