ios - NSFetchController 和 coredata 中的多对多

标签 ios objective-c core-data nspredicate

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

enter image description here

我不知道如何使用NSFetchRequest获取具体用户的好友

我有两个“对多”关系(见上文),我尝试使用 NSPredicate 结交 friend ,但最后数组是空的。

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];

最佳答案

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

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

关于ios - NSFetchController 和 coredata 中的多对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21704068/

相关文章:

ios - EZAudio 中的音频输入源

ios - 如何在后台处理/存储推送内容到核心数据?

ios - 应更新数据时 NSManagedObject 的删除/插入与更新

ios - UICollectionView 刷新问题

ios - 我应该为 ios 开发 native 使用哪种图像格式? SVG 还是 PNG?

ios - 如何在 UITextField iOS Swift 中添加连字符?

iphone - 尝试在我的应用程序中实现网络错误警报?

iOS 应用程序不与动态链接 URL 前缀关联

iphone - iOS : formatting decimal numbers

ios - Swift - 按部分对核心数据进行排序