If NSFetchRequest is the Core Data equivalent of an SQL query containing SELECT, is there a Core Data equivalent of the SQL command UPDATE?
Or in other words, if I want to change values on a Core Data set, is there a way around iterating over the results of an NSFetchRequest?
In case you’re answering with ‘no’, I’d very much appreciate some kind of reference documentation. Cheers!
EDIT: By ‘iteration’ I mean that I know how to for-loop over the entities and changes values:
NSArray *results = [context executeFetchRequest:...];
for (NSManagedObject *object in results) {
object.value = anotherValue;
}
But if they’re all getting the same value on the same property, I’d just like to call something like [context executeChangesOnFetchRequest:...]. Of course this method does not exist, but maybe there is a more elegant/concise way than the for-loop.
Well, no. If you want changes to be made on the object context in which the Core Data objects reside, you must set the respective properties on said objects and then commit the changes on that object context. Core Data will handle all the optimizations for you.
If you’re simply looking for a way to avoid a
forloop to update properties on objects in the result set, you can try usingmakeObjectsPerformSelector:withObject:.For the adventurous, you can try adding a category on
NSManagedObjectContext:You could then call this like so: