我正在尝试使用谓词来获取我要搜索的数据所在的数据NSSet
@property (nonatomic, retain) NSSet *categories;
@property (nonatomic, retain) NSString *otherDetails;
@property (nonatomic, retain) NSNumber *id;
NSSet
类别由另一个实体组成,包含:
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSNumber *id;
然后尝试通过谓词获取此内容
NSFetchRequest *fetcher =[NSFetchRequest fetchRequestWithEntityName:@"MyEntity"];
fetcher.predicate = [NSPredicate predicateWithFormat:@categories in %@", catList];
其中catList为:
NSmutableArray *catList = [[NSMutableArray alloc] init];
并填充数据
然后这给了我以下错误
'NSInvalidArgumetException', reason: 'unimplemented SQL generation for predicate: (categories IN {"this", "that"});
我尝试将其更改为使用NSSet
字段,例如
fetcher.predicate = [NSPredicate predicateWithFormat:@categories.name in %@", catList];
但这不起作用。
如果我更改谓词以使用NSString
它不会出错,但由于数据不同,不会返回任何结果。
fetcher.predicate = [NSPredicate predicateWithFormat:@categories in %@", catList];
那么我该怎样做才能使用谓词在NSSet
上进行查询?
更新: 我已经发现这个How to check if an objects is inside of a NSSet of objects by using predicateWithFormat in CoreData?可能会解决发布的问题,但我已经过多地简化了这个问题。
我真正拥有的是另一个拥有我所搜索的大量详细信息的实体,然后与我的“MyEntity”实体有多对一的关系。
MySearchEntity:
@property (nonatomic, retain) NSNumber *searchField1;
@property (nonatomic, retain) NSNumber *searchField2;
@property (nonatomic, retain) MyEntity *myEntity;
@property (nonatomic, retain) NSNumber *id;
和像这样的fetchrequest
NSFetchRequest *fetcher =[NSFetchRequest fetchRequestWithEntityName:@"MySearchEntity"];
fetcher.predicate = [NSPredicate predicateWithFormat:@"myEntity.otherDetails CONTAINS %@ && searchField1 <= %f && searchField2 >= %f && myEntity.categories.name in %@", catList];
因此声明的其余部分有效但我仍然无法通过MyEntities过滤它与类别的多对多关系。
答案 0 :(得分:5)
要查找相关MySearchEntity
对象在给定集合中至少有一个类别的MyEntity
个对象,您可以使用谓词
NSArray *catList = array of Category objects;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY myEntity.categories IN %@", catList];
或者,如果类别是按名称给出的:
NSArray *catList = array of NSString objects;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY myEntity.categories.name IN %@", catList];