views:

47

answers:

1

I'm getting some data from the web service and saving it in the core data. This workflow looks like this:

  1. get xml feed
  2. go over every item in that feed, create a new ManagedObject for every feed item
  3. download some big binary data for every item and save it into ManagedObject
  4. call [managedObjectContext save:]

Now, the problem is of course the performance - everything runs on the main thread. I'd like to re-factor as much as possible to another thread, but I'm not sure where I should start. Is it OK to put everything (1-4) to the separate thread?

+2  A: 

Yes, I recommend reviewing both Apple's Docs on multi-threaded Core Data and my article on the MDN (Mac Developer Network) http://www.mac-developer-network.com/columns/coredata/may2009/ which discuss the things you need to avoid and how to set everything up.

BTW, saving a lot of binary data into a Core Data object is generally a bad idea. The rule goes:

  • < 100KB save in the entity
  • < 1MB save in a separate entity hanging off a relationship
  • > 1MB save to disk and store its path into the managed object

Therefore you could spin off the download of the binary data into separate threads, save them to disk and then tell the main thread the NSManagedObjectID of the referencing object and the path and let the main thread do the very quick and easy linking. That would let your Core Data implementation stay single threaded and only spin off the data downloads.

Marcus S. Zarra