title: Core Data update if exists or create new managed object. Can I make this faster?
body: <p>I have the following code, and I was wondering if theres any way to make this faster. Basically my app downloads some JSON (about 4000 records) from the net, and updates or creates my managed objects based on the data. At the moment it's quite slow, and I can see why, but I'm new to core data so I was wondering if there's anything I can do to make it faster?</p>
  
  <pre><code> NSFetchRequest *request = [[NSFetchRequest alloc] init];
   NSEntityDescription *entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:managedObjectContext];
   [request setEntity:entity];
   NSMutableArray *coreDataArray = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
   [request release];
  
   for (NSDictionary *dict in arr) {
    NSArray *filtered = [coreDataArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(code == %@)", [dict objectForKey:@"Code"]]];
    //NSLog(@"COREDATA ARRAY: %d FILTERED ARRAY: %d CODE: %@ COREDATA FIRST CODE: %@", [coreDataArray count], [filtered count], [dict objectForKey:@"Code"], [[coreDataArray objectAtIndex:0] code]);
    if ([filtered count] &gt; 0) {
     Company *c = [filtered objectAtIndex:0];
     if ([dict objectForKey:@"Defunct"]) {
      NSLog(@"DELETED DEFUNCT COMPANY");
      [managedObjectContext deleteObject:c];
     } else {
      [c populateWithJSONDictionary:dict];
     }
    } else {
     Company *c = (Company *)[NSEntityDescription insertNewObjectForEntityForName:@"Company" inManagedObjectContext:managedObjectContext];
     [c populateWithJSONDictionary:dict];
    }
  
    float percent = (float)[arr indexOfObject:dict]/[arr count];
    [self performSelectorInBackground:@selector(updateProgressView:) withObject:[NSString stringWithFormat:@"%f",percent]];
   }
  
   [coreDataArray release];
  </code></pre>
  
  <p>Many thanks for any help you can give.</p>