如何形成谓词从核心数据中获取包含来自数组的string元素的对象

我需要创build一个谓词来从核心数据中获取对象,我的实体“Form”看起来像这样:

@interface EEForm (CoreDataProperties) @property (nullable, nonatomic, retain) NSDate *date; @property (nullable, nonatomic, retain) NSString *descriptor; @property (nullable, nonatomic, retain) NSString *location; @property (nullable, nonatomic, retain) NSMutableSet<Participants *> *participants; 

实体“参与者”看起来像这样:

 @interface Participants (CoreDataProperties) @property (nonatomic, assign) NSInteger indexRow; @property (nullable, nonatomic, retain) NSString *participant; @property (nullable, nonatomic, retain) EEForm *form; 

我想在参与者字段的基础上获取对象,该字段包含来自给定数组的所有对象(该数组由string对象组成,并且依赖于用户select的内容而变化)。 获取请求由FetchResultController执行。 我在初始化中设置谓词。

我是这样做的,但它包含了所有包含给定数组中至less一个对象的对象。

 - (NSFetchedResultsController *)fetchResultController { if(_fetchResultController != nil) { return _fetchResultController; } NSPredicate *lPredicate = [NSPredicate predicateWithFormat:@"ANY participants.participant IN %@", _selectedForPredicatel]; NSFetchRequest *lRequest = [[NSFetchRequest alloc]init]; [lRequest setPredicate:lPredicate]; NSEntityDescription *lEntity = [NSEntityDescription entityForName:@"Form" inManagedObjectContext:self.managedObjectContext]; [lRequest setEntity:lEntity]; [lRequest setFetchBatchSize:10]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"date" ascending:YES]; NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil]; [lRequest setSortDescriptors:sortDescriptors]; NSFetchedResultsController *fetchResultController = [[NSFetchedResultsController alloc]initWithFetchRequest:lRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; fetchResultController.delegate = self; self.fetchResultController = fetchResultController; NSError *error = nil; if(![[self fetchResultController] performFetch:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return fetchResultController; } 

你能否请教如何正确设置谓词来筛选包含给定数组中所有元素的对象。

编辑:用于谓词的数组看起来像这样:

  _selectedForPredicatel = [[NSMutableArray alloc]initWithObjects:@"Child",@"Adult", @"Group of people", @"Family", @"Teenager", nil]; 

每次用户selectfilter“Form”对象的参数,这个数组正在更新,所以我需要根据它来获取请求。 所以如果'Form'包含参与者:@“Child”,@“Adult”,但是在一个数组_selectedForPredicatel中有对象:@“Adult”,@“Group of people”,在这种情况下,这个“Form”不应该被抓取。 只应该被提取的'Form'包含_selectedForPredicatel数组的两个元素。 非常感谢您的任何build议。

如果对于每个用户的select至less有一个participant属性匹配的Participant ,则您希望包括这些表单。

如果参与者名称是唯一的(至less对于每个Form ),则可以计数匹配并与用户select的计数进行比较。 但是,如果不是,你需要分别创build一个谓词testing每个用户的select,然后将结果合并成一个大的谓词:

 NSPredicate *template = [NSPredicate predicateWithFormat:@"(SUBQUERY(participants, $p, $p.participant == $SELECTED).@count > 0)"]; NSMutableArray *predicateArray = [NSMutableArray array]; for (NSString *testString in _selectedForPredicatel) { NSDictionary *subsVars = @{@"SELECTED" : testString}; NSPredicate *testPredicate = [template predicateWithSubstitutionVariables:subsVars]; [predicateArray addObject:testPredicate]; } NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicateArray]; NSLog(@"finalPredicate is %@", finalPredicate); NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"ListEntity"]; fetch.predicate = finalPredicate; 

这里的template是一个虚谓词,使用$SELECTED作为用户select的占位符。 for循环使用它为每个用户的select创build一个谓词并将其添加到一个可变数组中。 最后的谓词是通过使用AND子句复合谓词数组来构build的。

这非常麻烦(最终的谓词足够糟糕,底层的SQL是可怕的),所以性能可能会很糟糕。

而不是任何你可以使用ALL:

  NSPredicate *lPredicate = [NSPredicate predicateWithFormat:@"ALL participants.participant IN %@", _sortByParticipants]; 

这将检查是否所有对象都在您的collections中。