NSFetch Controller和核心数据中的多对多

时间:2014-02-11 14:04:44

标签: ios objective-c core-data nspredicate

为了更好地理解我的问题想象你的Facebook朋友;我有一个实体“用户”,我用它来显示朋友之间的关系;

enter image description here

我不知道如何使用NSFetchRequest

获取具体用户的朋友

我有两个“多对多”的关系(见上文),我尝试使用NSPredicate来吸引朋友,但最后数组是空的。

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = (NSEntityDescription *)[_dbManager userEntity];
[fetchRequest setEntity:entity];

User *userByUid = [_dbManager userByUid:_currentUid];

NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                          initWithKey:@"sortId" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(friends IN %@)", userByUid];

[fetchRequest setPredicate:predicate];

[fetchRequest setFetchBatchSize:10];

NSArray *array = [_fetchedResultsController.managedObjectContext executeFetchRequest:fetchRequest error:nil];

1 个答案:

答案 0 :(得分:2)

您必须使用反向关系和“ANY”:

[NSPredicate predicateWithFormat:@"ANY beFriendsWith == %@", userByUid]