核心数据谓词一对多和多对一问题

发布于 2024-11-06 04:48:52 字数 1329 浏览 3 评论 0原文

我有客户实体和工作实体。

每项工作可以有一个客户。客户端作业的关系(客户端>>作业)称为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开柳相依 2024-11-13 04:48:52

jobOfClient 将返回一个对象集合,这意味着您的谓词本质上是在做:

NSSet *jobs = [thisObject jobOfClient];
return ([jobs isEqual:aJob]);

显然,集合永远不等于单个值,因此 CoreData 无法识别该谓词。为了解决这个问题,我认为您可以这样做:

[NSPredicate predicateWithFormat:@"jobOfClient CONTAINS %@", [userState selectedJob]];

为了确保您不会再次遇到这种情况,我建议将此关系的名称从 jobOfClient 更改为 jobs (使用复数形式表示它是一对多关系,并消除 OfClient,因为它已经在 Client 实体上)。您的其他关系可能也应该类似地重命名。 clientOfJob => 客户端

jobOfClient will return a collection of objects, which means your predicate is essentially doing:

NSSet *jobs = [thisObject jobOfClient];
return ([jobs isEqual:aJob]);

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:

[NSPredicate predicateWithFormat:@"jobOfClient CONTAINS %@", [userState selectedJob]];

And to make sure you don't run into this again, I would recommend changing the name of this relationship from jobOfClient to just jobs (using the plural form to indicate it's a to-many relationship, and eliminating the OfClient, because it's already on the Client entity). Your other relationships should probably be similarly renamed. clientOfJob => client, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文