Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetching objects from core data not in a set

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 :)

like image 203
tomas Avatar asked May 31 '11 00:05

tomas


People also ask

How can you retrieve data from Core Data?

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.

What is fetched property in Core Data?

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.

What is Nsfetchrequest?

A description of search criteria used to retrieve data from a persistent store.


2 Answers

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

like image 110
justin Avatar answered Oct 02 '22 18:10

justin


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]; //...    
like image 21
mutlugokhan Avatar answered Oct 02 '22 18:10

mutlugokhan