Search code examples
ioscore-dataios7core-data-migration

Bulk update data in iOS 7 CoreData


Is there way to run updating of all objects for some entity by one SQL-query? Not to fetch and run-looping.

For example like to run

UPDATE someEntity SET filed1 = value1 WHERE field2 = value2

Solution

  • Mercelo's answer will not work for iOS-7 for this you can

    NSFetchRequest *fetchAllRSSItems = [NSFetchRequest fetchRequestWithEntityName:[RSSItem entityName]];
    NSError *fetchError;
    NSArray *results = [self.managedObjectContext executeFetchRequest:fetchAllRSSItems error:&fetchError];
    
    if (results == nil) {
    
        NSLog(@"Error: %@", [fetchError localizedDescription]);
    } else {
    
        for (RSSItem *rssItem in results) {
            rssItem.read = [NSNumber numberWithBool:YES];
        }
    
        [self saveManagedObjectContext];
    }