Search code examples
iosobjective-ccore-dataaddressbook

Syncing Core Data With AddressBook


Whats the best way to purge all data i have in my core data file, and rewrite my updated addressbook to my core data? Is there a way to overwrite data in core data? how can i do this in a good practice?

I want to be able to sync my addressbook and my core data file, one of the methods will be to check on what changed in address book, and delete or add records in my core data according to that change ( i don't care about edited records ), and one method will be to just delete all my data in my SQLite file with core data and save my updated addressbook again to the same file.


Solution

  • See this blog post from Brent Simmons and my comment on my answer to your previous question.

    Deleting a bunch of objects from a Core Data store is expensive. You must fetch and then instantiate each NSManagedObject, then delete it, then push the changes to your Core Data stack. This process takes much more time than a simple SQL DELETE would take.

    If you have other data in the Core Data store that must be preserved, then you're stuck. You have to do it this way. But if you can blow away your datastore and start over, that will be much faster. Create a new Core Data store with the current Address Book data. Save it. Close your existing datastore. Move the new datastore to the location of the old one (or change the file path to the new store's path).

    The bulk update feature in iOS 8 will probably speed this process.