swalkner
swalkner

Reputation: 17329

Core Data: move object from one persistent store to another

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:

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?

Upvotes: 3

Views: 1777

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70936

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.

Upvotes: 3

Related Questions