我的核心数据模型中有 2 个表。
项目和过滤器
FILTER有很多ITEM
我正在尝试使用表 FILTER isSelected == 1 的参数获取所有项目。
我有这个谓词,但它不起作用,你能帮我如何获得我想要的元素吗? 我正在使用 MagicalRecord
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"filter.isSelected == 1"];
NSArray *items = [Item MR_findAllWithPredicate:predicate];
self.itemList = [items copy];
最佳答案
试试这个,我在我的应用程序中为 Item 和 Department 做同样的事情。
Filter *department=nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Filter" inManagedObjectContext:__managedObjectContext];
[fetchRequest setEntity:entity];
Item *anItem=nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:__managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isSelected==%d and isSelected==%@", [department.isSelected integerValue ], @"1"];
[fetchRequest setPredicate:predicate];
NSArray *resultSet = [UpdateManager executeForContext:self.managedObjectContext FetchRequest:fetchRequest];
resultSet 是你的结果数组。
试着让我知道。
关于ios谓词一对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23488067/