iosiphoneobjective-ccore-datansfetchrequest

core data - fetch attribute that match one of the values in an array


My core data model:

Person 
======
personId  (NSNumber)

This is a basic core data question,
I have an array of personIds (not Person, just NSNumber of ids) and I want to fetch all the Persons with corresponding id in the array.

This is how I fetch a person that correspond to one id:

   NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
   request.predicate = [NSPredicate predicateWithFormat:@"personId = %@", onePersonId];

I'm looking for a way to fetch multiple persons that match to multiple ids


Solution

  • Use 'IN' match for this:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"personId IN %@", idsArray];