dontWatchMyProfile
dontWatchMyProfile

Reputation: 46310

Can I clear the Managed Object Context?

Problem: I am doing a really big import where I parse an XML file. For every 10 parsed managed objects, I want to save the Managed Object Context and get rid of those 10 objects in memory, so that I have never more than 10 objects in memory at a time.

After saving it, how could I clear the context so that all the objects go away from memory?

Upvotes: 11

Views: 5641

Answers (1)

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

In a situation like this there are four things to remember to do:

  1. Wrap your loop in a NSAutoreleasePool
  2. Periodically save the context; then
  3. Reset the context with -reset
  4. Release and re-create the autorelease pool

This will flush all of the memory being used and clear the context.

Upvotes: 26

Related Questions