I'm trying to fetch objects from core data that are not in a given set, but I haven't been able to get it to work.
For instance, suppose that we have a core data entity named User, which has a few attributes such as userName, familyName, givenName, and active. Given an array of strings representing a set of usernames, we can easily fetch all the users corresponding to that list of usernames:
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:moc]; [request setEntity:entity]; NSArray *userNames = [NSArray arrayWithObjects:@"user1", @"user2", @"user3", nil]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userName IN %@", userNames]; [request setPredicate:predicate]; NSArray *users = [moc executeFetchRequest:request error:nil];
However, I want to fetch the complement of that set, i.e., I want all the users in core data that don't have the usernames specified in the userNames array. Does anyone have an idea how to approach this issue? I thought it would be simple enough to add a "NOT"
in the predicate (i.e., "userName NOT IN %@")
, but Xcode throws an exception saying the predicate format could not be parsed. I also tried using the predicate builder available for fetch requests with no luck. The documentation wasn't particularly helpful either. Suggestions? Comments? Thanks for all your help :)
You should load all your Objects from CoreData into an Array/Dict of NSManaged Objects. executeFetchRequest() returns an optional which is nil if the fetch request fails. If you forcefully cast to [Locations] then the app will crash in that case.
Fetched Properties in Core Data are properties that return an array value from a predicate. A fetched property predicate is a Core Data query that evaluates to an array of results.
A description of search criteria used to retrieve data from a persistent store.
In order to find the objects that aren't in your array, all you have to do is something like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (userName IN %@)", userNames];
That should return a request of all the objects without the ones you specified
I am not strong at core data/objective-c but the predicate should be like the following statement;
[predicateFormat appendFormat:@"not (some_field_name in {'A','B','B','C'})"];
An example:
NSMutableString * mutableStr = [[NSMutableString alloc] init]; //prepare filter statement for (SomeEntity * e in self.someArray) { [mutableStr appendFormat:@"'%@',", e.key]; } //excluded objects exist if (![mutableStr isEqual:@""]) { //remove last comma from mutable string mutableStr = [[mutableStr substringToIndex:mutableStr.length-1] copy]; [predicateFormat appendFormat:@"not (key in {%@})", mutableStr]; } //... //use this predicate in NSFetchRequest //fetchRequest.predicate = [NSPredicate predicateWithFormat:predicateFormat]; //...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With