Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data: move object from one persistent store to another

Tags:

core-data

ios5

I'm using Core Data in my app and would like to export only some of the data and import it on some other device.

To avoid migration issues, I'd like to do the following:

Export:

  • create a second export.sqlite-file with the same database model, but empty
  • add that file with addPersistentStoreWithType
  • copy some ManagedObjects over to that .sqlite
  • remove the added persistent store

Import: - copy export.sqlite-file into app - add that .sqlite-file with addPersistentStoreWithType - copy data over - remove added persistentStore

but how to achieve that? i.e. how can I tell my managed object so copy itself into the other store?

like image 692
swalkner Avatar asked Apr 14 '26 15:04

swalkner


1 Answers

how can I tell my managed object so copy itself into the other store?

You can't, not directly anyway. You'll have to do something like:

  • For each object in the origin data store,
    • Create a new object in the target store with the same entity type
    • Assign the new object's attributes to the same values as the original object
  • Once you're done creating new objects, do a second pass to set up any relationships.

The relationships need to be done separately, because all of the objects in a relationship need to exist before you can create the relationship.

like image 95
Tom Harrington Avatar answered Apr 17 '26 12:04

Tom Harrington



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!