为了更好地理解我的问题想象你的Facebook朋友;我有一个实体“用户”,我用它来显示朋友之间的关系;
我不知道如何使用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];
答案 0 :(得分:2)
您必须使用反向关系和“ANY”:
[NSPredicate predicateWithFormat:@"ANY beFriendsWith == %@", userByUid]