2010-07-09 70 views
1

因此,CoreData中的公司与员工之间存在一对多的关系(使用iOS上的SQLite后端,如果相关的话)。我想创建一个谓词,它只返回与其关联的员工数为0的公司。我可以通过所有的这些公司来迭代它们,但那会是(我假设)慢得多。如何确定CoreData中一对多关系中的对象数量

任何想法?

感谢,
-Aaron

回答

2

试图@ falconcreek答案,并得到一个错误后(在我评论他的回答说明),我做了一些谷歌搜索,并确定答案是

NSPredicate *noEmployeesPredicate = [NSPredicate predicateWithFormat:@"[email protected] == 0"]; 

现在一切正常übere fficiently。谢谢!

+0

优秀。我正在寻找文档中的@count函数。检查零关系也是值得的。 – falconcreek 2010-07-09 05:17:23

1

假设你的公司 - >雇员的关系被命名为“员工”

NSManagedObjectContext *moc = [self managedObjectContext]; 
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:moc]; 
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; 
[request setEntity:entityDescription]; 

// the following doesn't work 
// NSPredicate *noEmployeesPredicate = [NSPredicate predicateWithFormat:@"employees = nil OR employees[SIZE] = 0"]; 

// use @count instead 
NSPredicate *noEmployeesPredicate = [NSPredicate predicateWithFormat:@"employees = nil OR [email protected] == 0"]; 
[request setPredicate:predicate]; 

NSError *error = nil; 
NSArray *array = [moc executeFetchRequest:request error:&error]; 
if (error) 
{ 
    // Deal with error... 
} 
+0

感谢您的建议。不幸的是,当我运行它时,我得到了''NSInvalidArgumentException',原因:'对多密钥不允许在这里' 这导致了一些Google搜索,这导致我发布的结果作为答案。我认为问题在于我使用的是一对多关系。不管怎样,非常感谢! – 2010-07-09 05:03:57

相关问题