核心数据谓词一对多和多对一问题
我有客户实体和工作实体。
每项工作可以有一个客户。客户端作业的关系(客户端>>作业)称为jobOfClient。
每个客户可以有很多工作。作业的客户端的关系(作业<<-->客户端)称为clientOfJob。
(当然,这些是逆关系。)
我有一些谓词有效,但最后一个没有。省略一些 fetchedResultsController 设置,以下是三种情况的一些关键代码行:
此处,我对作业进行排序,查找与任何客户端无关的作业:
NSEntityDescription * entity = [NSEntityDescription entityForName:@"Job" inManagedObjectContext:dataInterface.managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"clientOfJob == nil"];
此处,我对作业进行排序,查找作业特定客户的:
NSEntityDescription * entity = [NSEntityDescription entityForName:@"Job" inManagedObjectContext:dataInterface.managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"clientOfJob == %@", userState.selectedClient];
但是下一个不起作用。在这里,我对客户进行排序,查找与所选作业关联的一个客户(如果没有相关客户,则不返回任何结果,但这里不是这种情况)。
NSEntityDescription * entity = [NSEntityDescription entityForName:@"Client" inManagedObjectContext:dataInterface.managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"jobOfClient == %@", userState.selectedJob];
错误消息是由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“此处不允许使用多对键”
这里一定有一些我不明白的微妙之处。有人可以帮助我**我提供的信息吗?
I have Client entities and Job entities.
Each job can have one client. The relationship for the jobs of a client (client<-->>job) is called jobOfClient.
Each client can have many jobs. The relationship for client of a job (job<<-->client) is called clientOfJob.
(Of course, these are inverse relationships.)
I have some predicates that are working, but the last one does not. Leaving out some of the fetchedResultsController set up, here are some of the key lines of code for three cases:
Here, I sort through jobs, looking for jobs that aren't related to any client:
NSEntityDescription * entity = [NSEntityDescription entityForName:@"Job" inManagedObjectContext:dataInterface.managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"clientOfJob == nil"];
Here I sort through jobs, looking for jobs of a particular client:
NSEntityDescription * entity = [NSEntityDescription entityForName:@"Job" inManagedObjectContext:dataInterface.managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"clientOfJob == %@", userState.selectedClient];
But this next one doesn't work. Here I sort through clients, looking for the one client associated with a selected job (or return no result if there is no related client, but that's not the case here).
NSEntityDescription * entity = [NSEntityDescription entityForName:@"Client" inManagedObjectContext:dataInterface.managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"jobOfClient == %@", userState.selectedJob];
The error message is Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'
There must be something subtle here that I don't understand. Can someone help me with **the info I have given?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jobOfClient 将返回一个对象集合,这意味着您的谓词本质上是在做:
显然,集合永远不等于单个值,因此 CoreData 无法识别该谓词。为了解决这个问题,我认为您可以这样做:
为了确保您不会再次遇到这种情况,我建议将此关系的名称从
jobOfClient
更改为jobs (使用复数形式表示它是一对多关系,并消除
OfClient
,因为它已经在Client
实体上)。您的其他关系可能也应该类似地重命名。clientOfJob
=>客户端
等jobOfClient
will return a collection of objects, which means your predicate is essentially doing:Obviously, a collection is never equal to a single value, and so CoreData does not recognize the predicate. To get around this, I think you can do:
And to make sure you don't run into this again, I would recommend changing the name of this relationship from
jobOfClient
to justjobs
(using the plural form to indicate it's a to-many relationship, and eliminating theOfClient
, because it's already on theClient
entity). Your other relationships should probably be similarly renamed.clientOfJob
=>client
, etc.