Search code examples
iosobjective-cdatabasecore-datansmanagedobjectcontext

Reduce NSManagedObjectContect save time


I am trying to make my algorithm faster as I am seeing long loading times. My app loads thousands of objects from an external database and then saves it on the device. I ran time profiler with my ipod touch and I saw that 17 seconds/26% of my loading time is spent executing NSManagedObjectContexts' save: function.

I am using one privacte nsmanagedobjectcontext and one persistant store coordinator.

What affects the time spent saving? If I save 1/10th of the data 10 times instead of all of the data once, which one is faster? What can I try to optimize the time spent saving?

enter image description here


Solution

  • You are on the right track. The best lever I have found for optimizing Core Data save times is indeed finding the right batch size.

    As suggested by one commenter, the best way is to test it out. There are no definite rules because it really depends on the nature of your data, the size and number of the records etc.

    Other strategies include using @autorelease pool(s) which will optimize memory usage and might improve performance as well.

    Let us know your results!