filtering an array of entities using NSPredicate
filtering an array of entities using NSPredicate
- Subject: filtering an array of entities using NSPredicate
- From: Koen van der Drift <email@hidden>
- Date: Sat, 13 Oct 2012 17:23:42 -0400
I have a large array for Person entities, and want to filter them before importing into my Core Data database, so that each Person will be only once in the database based on the name. The names are pulled from a database, and have the format: @"Jones, A."
After some searching, I came up with the following:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity: [NSEntityDescription entityForName: @"Person" inManagedObjectContext: context]];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat: @"name IN %@", persons]];
NSError *error;
NSArray *filteredPersons = [context executeFetchRequest: fetchRequest error: &error];
// get the difference
NSMutableArray *uniquePersons = [NSMutableArray arrayWithArray: persons];
[uniquePersons removeObjectsInArray: filteredPersons];
And then import them.
As it turns out, filteredPersons is always empty, even though I know certain names are already present
How can I make this work?
Thanks,
- Koen.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden