views:

27

answers:

2

I've googled and researched but am not able to find the best way to iterate through all or some managed objects in my Data Model and update an attribute value in each of them, in my situation, update to the current date. The managed object context and persistent store delegate methods are all in my Application Delegate. I could add some code in a table view in the program but I feel it would be more efficient to call method to update these values, as they may not necessarily return to the table view.

Ultimately I want to loop through my managed objects and update values as I go through the loop, from anywhere in my application.

Please let me know if you require any more information. Thanks!

-Coy

+2  A: 

The only way to update multiple objects is to have all of those objects. There is no way to do "batch updating" with Core Data like you can in SQL, because Core Data is not a database. It is an object graph persistence framework, which means you will deal with objects.

There are tricks you can do to keep your memory usage down (such as batching your fetch request and only fetching certain properties), but ultimately you will be using a loop and updating the objects one-by-one.

Dave DeLong
A: 

Depending on what you want to update the values to, you could fetch an array of objects and call setValue:forKey: on the array, which would set the value for every object in the array. e.g.:

//fetch managed objects
NSArray *objects = [context executeFetchRequest:allRequest error:&error];
[objects setValue:newValue forKey:@"entityProperty"];
//now save your changes back.
[context save:&error];

Setting up the fetch request should just require the entity you want, leave the predicate nil so that you get all instances back.

ImHuntingWabbits
Excellent! I didn't realize it was as simple as that. Thank you all for your help. This answer resolved my issue.
Coy