我有核心数据实体,个人和边界。他们之间有着多到多的关系(每个人可以有多个边界,每个边界可以有很多人)。
我正在尝试创建一个清单,列出弗莱德也没有恋爱的界限。
Person *person = [Person MR_findFirstByAttribute:@"name" withValue:@"Fred"];
DLog(@"person.boundaries.count: %d", person.boundaries.count);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY persons != %@", person];
DLog(@"testBoundaries.count: %d", [Boundary MR_countOfEntitiesWithPredicate:predicate]);
我在数据库中有47个边界,弗雷德可以看到所有47个。因此,我希望我的fetch返回0:
DEBUG | -[LoginViewController viewDidLoad] | person.boundaries.count: 47
DEBUG | -[LoginViewController viewDidLoad] | testBoundaries.count: 47
我的谓词怎么了?
发布于 2013-10-31 21:04:12
[NSPredicate predicateWithFormat:@"ANY persons != %@", fred]
查找与弗雷德其他人相关的所有对象。你想要的是
[NSPredicate predicateWithFormat:@"NOT(ANY persons = %@)", fred]
这将返回与Fred无关的所有对象。
然而,似乎存在一个核心数据错误,在谓词中"NONE“或NONE不能正确工作,比较NSPredicate Aggregate Operations with NONE。解决方法是使用SUBQUERY:
[NSPredicate predicateWithFormat:@"SUBQUERY(persons, $p, $p == %@).@count == 0", fred]
发布于 2013-10-31 21:00:17
您需要首先在Class Boundary
中询问是否有一个名为弗雷德的人是有边界的。应该是这样的:
NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Boundary"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"ANY persons.name != %@", person.name];
这将获取弗雷德没有的所有边界。
https://stackoverflow.com/questions/19716276
复制相似问题