0

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.

Moody
  • 352
  • 3
  • 15

1 Answers1

0

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.

Community
  • 1
  • 1
Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
  • This is quite frustrating, since Addressbook doesn't tell what kind of changes happened to it, and i can't easily overwrite my core data file. So i will have to manually search for the added/deleted rows in my AddressBook and compare that to my core data file, and add/delete according to the change was done in AddressBook. Thats the best way to do it? Note: My Core Data File consists of all my addressbook contacts IDs only, and a boolean attribute. It could expand later on but for now thats how it is. – Moody Jul 16 '14 at 11:59
  • I think that in general, that's the best way. Do it in a background thread (or -performBlock) to minimize any UI blockage. Use the modification dates! Also remember that uniquing is best done outside of Core Data. – Hal Mueller Jul 16 '14 at 12:05