How to generate a predicate for obtaining objects from Core Data containing string elements from an array

I need to create a predicate to get an object from the main data. My "Form" entity looks like this:

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

The Entity Entity looks like this:

@interface Participants (CoreDataProperties)

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

I want to get objects based on the participants field, which contains all the objects from this array (this array consists of string objects, and it changes depending on what the user selects). The Fetch request is executed by the FetchResultController. I set the predicate to its initialization.

I did it this way, but it includes all objects containing at least one object from this array.

- (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;
}

Could you advise how to correctly set the predicate for filtering objects containing all elements from this array.

EDIT: , , :

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

, , "Form", , , . , "" : @ "", @ "", _selectedForPredicatel : @ "", " ", "" . , "Form" _selectedForPredicatel. .

+1
2

, Participant, Participant, .

( , Form), . , , :

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 . for . AND.

( , SQL ), .

+1

ALL:

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

, .

+1

All Articles