Question

You're not supposed to modify the collection being iterated upon with fast enumeration, but I'm not really sure to what extent that is. The below code has not caused me any problems, but I'm not sure if it was me being lucky. Does anyone have a defininite answer?

for(NSManagedObject *myObject in myArray) {
   [myObject.managedObjectContext deleteObject:myObject];
}

So I'm not really modifying the array, but I am deleting the object from the context, which might just indirectly modify the array, but I'm not sure on that..

Was it helpful?

Solution

You can do anything you want with the objects in the array, so long as you do not mutate the array itself.

That code is perfectly acceptable.

Note, however, if you have lots of objects to delete, you should consider other things as well (like prefetching relationships, using an autorelease pool, making sure your MOC stays clean, etc.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top