title: Deleting/removing records in Core Data very very slow
body: <p>I am putting the finishing touches on an App, and have difficulties with removing records in bulk. On the hit of a button a set of approx. 3500 records need to be added to the database. This is not a problem, and takes approx. 3-4 seconds.</p>
  
  <p>But sometimes (not often, but the option needs to be there) all these records need to be removed. I just ran this operation, and it took 20 minutes. What could be wrong here? There is only one dependency, all the records are children of a particular Collection.</p>
  
  <p>I add all the items to a set, removed them from the Collection and then delete one by one. Every 5% I update the dialogue and when everything is done I commit the changes. But removing the items just takes ages (as I can see the progress dialogue progress very slowly)</p>
  
  <pre><code>- (void) deleteList:(DOCollection *) collection {
  
      // For the progress dialogue
      NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithObject:@"Clearing vocabulary list!" forKey:@"message"];
      float totalItems = [collection.items count];
      float progress = 0;
      float nextProgressRefresh = 0.05;         
  
      NSMutableSet* itemsSet = [NSMutableSet set];
      for (DOItem* item in collection.items) {
          [itemsSet addObject:(NSNumber*)[NSNumber numberWithInt:[item.itemId intValue]]];
      }
  
      // Remove all of them from the collection
      [managedObjectContext performBlockAndWait:^{
          [collection setItems:[NSSet set]];
      }];    
  
      for (NSNumber* itemId in itemsSet) {
          DOItem* item = [itemController findItem:[itemId intValue]];
          if (item != nil) {
             [[self itemController] removeItem:item];
          }
  
          progress++;
          if((nextProgressRefresh &lt; (progress / totalItems))){  
              NSString* sProgress = [NSString stringWithFormat:@"%f", (progress / totalItems) * 0.85];
              //[dict setValue:@"Saving the database...!" forKey:@"message"];
              [dict setValue:sProgress forKey:@"progress"];
              [[NSNotificationCenter defaultCenter] postNotificationName:kUpdatePleaseWaitDialogue object:dict]; 
              nextProgressRefresh = nextProgressRefresh + 0.05;
          }        
      }
  
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
          [managedObjectContext performBlockAndWait:^{
              [[self collectionController] commitChanges];
          }];
          [[NSNotificationCenter defaultCenter] postNotificationName:kSavingDataComplete object:nil]; 
      });  
  
      //NSLog(@"Wait 2!");
      [NSThread sleepForTimeInterval:1];    
  
  }
  </code></pre>
  
  <p>in DOItemController:</p>
  
  <pre><code>- (void) removeItem: (NSManagedObject*) item {
      [[self managedObjectContext] deleteObject:item];
  }
  </code></pre>