countForFetchRequest返回1为空表 [英] countForFetchRequest returning 1 for empty table

查看:213
本文介绍了countForFetchRequest返回1为空表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一个'countForFetchRequest',它总是返回比我的表中的记录数多1个。

  NSFetchRequest * request = [[NSFetchRequest alloc] init]; 
[request setEntity:[NSEntityDescription entityForName:@TESTDATAinManagedObjectContext:self.managedObjectContext]];

NSError * error = nil;
NSUInteger count = [self.managedObjectContext countForFetchRequest:request error:& error];
if(count == NSNotFound){
//处理错误
}

return count;

当我的表为空时返回1。如果我的表有3行,则返回4。

  sqlite>从ZTESTDATA中选择计数(*); 
0
sqlite>关于我在做什么的任何想法?

$ p


>正如其他人建议的那样,请执行获取而不是计数,并打印出Core Data找到的内容,您会找到您的答案。


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.

这篇关于countForFetchRequest返回1为空表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆