views:

185

answers:

1

I will be doing an occiasional import from XML into core data. I have around 50k entities that will be added. My question is how often should I call [managedObjectContext save:&error]. For every new entity added, or every x entities, or just at the end of the 50k import?

I currently am calling it for each entity and tried only doing it for around every 10k and import speed went up drastically but after the first 30k it would crash with:

*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <NSCFSet: 0x13e760> was mutated while being enumerated.'

Before I spend too much time trying to diagnose what is going on there I figured I'd check if its ok to not call save on every entity?

Is the # of entities before calling save limited by the amount of memory those entities are using?

+2  A: 

Do not save every entity, that is wasteful and inefficient. The sweet spot depends on your data but it is definitely greater than every entry and less than 50K :)

I would recommend starting at every 1k and adjust from there as you get further into the application's development.

As for that error you are seeing, it has little to nothing to do with saving the data. You are removing or adding objects to a mutable array while you are iterating over it. That is not allowed for obvious reasons.

Marcus S. Zarra