简体   繁体   English

NSSet的Coredata提取谓词

[英]Coredata fetch predicate for NSSet

I have a ManagedPhoto coredata object that contains a NSSet attribute called tags. 我有一个ManagedPhoto coredata对象,其中包含一个称为标签的NSSet属性。 Each object in the tags set is a NSString. 标签集中的每个对象都是一个NSString。

I need to fetch all ManagedPhoto objects that have tags with a specific value, say 'party'. 我需要获取所有具有特定值标签(例如“ party”)的ManagedPhoto对象。 This is what I'm doing - 这就是我正在做的-

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"ManagedPhoto"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"SELF.tags == 'party'"];
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];

But I always get an empty results array even though I know for sure that there are ManagedPhotos with tags containing 'party'. 但是,即使我确定有包含“ party”标签的ManagedPhotos,我也总是得到一个空结果数组。 I've tried this as well - 我也尝试过-

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"SELF.tags IN %@", @[@"party"]];

I've tried many other things as well but nothing has worked so far! 我也尝试了许多其他事情,但到目前为止没有任何效果! Any thought? 任何想法?

To fetch objects that have at least one tag with the specified value, the following predicate should work 要获取具有至少一个具有指定值的标签的对象,应使用以下谓词

[NSPredicate predicateWithFormat:@"ANY tags == 'party'"]

or better 或更好

[NSPredicate predicateWithFormat:@"ANY tags == %@", @"party"]

But I assume that the set is defined as a transformable property, which means that it is stored as a binary archive in the SQLite file. 但是我假设该集合被定义为可转换的属性,这意味着它以二进制存档的形式存储在SQLite文件中。 Then the above queries will work at most for objects already loaded into the managed object context, but not against the store file. 然后,上述查询最多将对已经加载到托管对象上下文中的对象起作用,但不适用于存储文件。

You probably should define tags as a to-many relationship to another entity which has a String property (eg "name") and then query with the predicate 您可能应该将tags定义为与另一个具有String属性(例如“ name”)的实体的多对多关系,然后使用谓词查询

[NSPredicate predicateWithFormat:@"ANY tags.name == %@", @"party"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM