开发者

countForFetchRequest behavior

I'm newbie in coredata and still learning. I have an application which need to fetch number of records which fulfill given criteria.

Assume I have 3 types of Entity, i.e. Entry, Tag and Attachment. Entry has to-many relationship with Tag and Attachment. Tag has to-one relationship with Entry. Attachment has to-one relationship with Entry.

For following code snippets:

NSFetchRequest *req = [[NSFetchRequest alloc] init];
NSEntityDescription entity = [NSEntityDescription entityForName:@"Entry" inManagedContext:ctx];
[req setEntity:entity];

NSPredicate *pred = [NSPredicate predicateWithFormat:@"(name like 'a*') AND (tags.name like 'test*') OR (attachments.name like 'photo*')"];
[req setPredicate:pred];

NSUInteger count1 = [ctx countForFetchRequest:req error:nil];
NSUInteger count2 = [[ctx executeFetchRequest:req error:nil] count];

Apparently the result returns by the 2 calls above开发者_运维问答 is different. The count1 result is wrong and count2 result is correct. count1 is showing result bigger than it supposed to be.

Anyone has clue why the result are different for above codes?

Thanks, Meiwin

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜