CoreData:多对多关系
我有 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您可能已经怀疑的那样,您的谓词有问题:
也许您因命名关系的方式而感到困惑。这些名称似乎表明存在一些概念上的混乱。让我建议您像这样重命名实体和关系:
有一个更好的解决方案。属性活动不是已经包含您想要的活动了吗?因此:
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:
There is a better solution. Does not the attribute activities already contain exactly the activities you want? Thus: