cocoa - Iterate through NSManagedObjectContext objects? -
i want iterate through of objects in nsmanagedobjectcontext, , update them manually. then, every managed object should updated. what's best way this?
theoretically iterate through entity descriptions in managed object model, build no-predicate fetch request them, loop on returned objects , update. example:
// given nsmanagedobjectcontext *context nsmanagedobjectmodel *model = [[context persistentstorecoordinator] managedobjectmodel]; for(nsentitydescription *entity in [model entities]) { nsfetchrequest *request = [[[nsfetchrequest alloc] init] autorelease]; [request setentity:entity]; nserror *error; nsarray *results = [context executefetchrequest:request error:&error]; // error-checking here... for(nsmanagedobject *object in results) { // updates here } }
note can cast nsmanagedobjects returned necessary either testing class equality (using iskindofclass:
or related method) or figuring out class current entity (using managedobjectclassname
property on entity
in conjunction nsclasswithname()
method).
Comments
Post a Comment