CoreData:多对多关系

发布于 2024-12-05 03:07:11 字数 963 浏览 0 评论 0原文

我有 2 个对象,具有多对多关系。

在此处输入图像描述

这是我的代码:

+ (NSSet *)activitiesSetForMember:(Members *)member inManagedObjectContext:(NSManagedObjectContext *)context{
NSSet *activitiesSet = nil;

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"CompanyActivity" inManagedObjectContext:context];
request.predicate = [NSPredicate predicateWithFormat:@"hasMembers contains %@", member];
NSError *error = nil;
activitiesSet = [[context executeFetchRequest:request error:&error] lastObject];
[request release];

NSLog(@"count: %d", [activitiesSet count]);

//se o array vem vazio (nil), não existe a o tipo de Membro
if (!error && !activitiesSet)
    NSLog(@"Error: Member has no Activities related.");

return activitiesSet;   
}

如何从选定的 < 中获取所有 Activities代码>会员?我该如何构建请求才能满足我的要求?

I have 2 objects, with many to many relationships.

enter image description here

Here's my code:

+ (NSSet *)activitiesSetForMember:(Members *)member inManagedObjectContext:(NSManagedObjectContext *)context{
NSSet *activitiesSet = nil;

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"CompanyActivity" inManagedObjectContext:context];
request.predicate = [NSPredicate predicateWithFormat:@"hasMembers contains %@", member];
NSError *error = nil;
activitiesSet = [[context executeFetchRequest:request error:&error] lastObject];
[request release];

NSLog(@"count: %d", [activitiesSet count]);

//se o array vem vazio (nil), não existe a o tipo de Membro
if (!error && !activitiesSet)
    NSLog(@"Error: Member has no Activities related.");

return activitiesSet;   
}

How can I get all the Activities from a selected Member? How can I construct the request do give me that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

笨死的猪 2024-12-12 03:07:11

正如您可能已经怀疑的那样,您的谓词有问题:

也许您因命名关系的方式而感到困惑。这些名称似乎表明存在一些概念上的混乱。让我建议您像这样重命名实体和关系:

Members         - Member
CompanyActivity - Activity
memberActivity  - activities
hasMembers      - members

有一个更好的解决方案。属性活动不是已经包含您想要的活动了吗?因此:

activitiesSet = member.activities; // according to my naming scheme

As you probably already suspected, something is wrong with your predicate:

Perhaps you are confused because of the way you named your relationships. The names seem to indicate that there is some conceptual muddiness. Let me suggest that you rename your entities and relationships like this:

Members         - Member
CompanyActivity - Activity
memberActivity  - activities
hasMembers      - members

There is a better solution. Does not the attribute activities already contain exactly the activities you want? Thus:

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