我的获取请求是获取所有组对象而不是我想要的组对象。
我希望仅获取当前用户所属的组,但我的谓词似乎不起作用。对错误的想法?
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"AGroup" inManagedObjectContext:_managedObjectContext]];
AUser *u = [AUser currentUser]; //gets app's current user
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"ANY users = %@", u]];
我也试过以下,但没有用:
[NSPredicate predicateWithFormat:@"(users CONTAINS %@)", u];
[NSPredicate predicateWithFormat:@"SUBQUERY(users, $user, $user = %@).@count = [users count]", u];
班级结构
AGroup
- users (Type: AUser *)
AUser
- name (Type: NSString *)
答案 0 :(得分:1)
我看不出原因
[NSPredicate predicateWithFormat:@"ANY users = %@", u]
对您不起作用,但更简单的解决方案就是使用反向关系
AUser *u = [AUser currentUser];
NSSet *theGroups = u.groups;
或者,如果您需要它作为数组:
NSArray *theGroups = [u.groups allObjects];