Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate check NSArray if object has one of several ID

Little diifficult to explain but I am trying to use NSPredicate for filtering an array with custom NSManagedObject by ids. I have a server that can send update, delete or add new objects, and I need to control if those objects from the JSON file already exist, if exist just update them or insert to core data if not.

I am using this predicate now :

   NSPredicate *predicate = [NSPredicate predicateWithFormat:@"storeId != %@", [jsonFile valueForKey:@"Id"];

Where jsonFile contains unparsed Store objects. But with this predicate, it will give me a huge array, since one id will be unlike some storeId, and next id will match.

Json file is some sort of this :

     "Stores":[{
          "id":1,
          "name":"Spar",
          "city":"London"
          }
          {
           "id":2,
           "name":"WalMart",
           "city":"Chicago"
       }];
like image 744
lagos Avatar asked Jul 30 '12 12:07

lagos


1 Answers

I am not sure if I understand correctly what you are trying to achieve, but perhaps you can use the following:

NSArray *jsonFile = /* your array of dictionaries */;
NSArray *idList = [jsonFile valueForKey:@"id"]; // array of "id" numbers
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT(storeId IN %@)", idList];

This will give all managed objects that have a storeId that is not equal to any of the ids in the jsonFile array.

like image 61
Martin R Avatar answered Nov 04 '22 19:11

Martin R