0

I'm looking to serialize a collection of Core Data objects (probably into JSON) and have been doing reading on the subject. I'll likely be serializing/deserializing JSON data as a means of allowing iOS users to easily export and re-import their data. This could be a case of backing up and restoring on the same device, or importing the data onto an entirely new device.

My main question at this point is how I handle the identification of existing data during import. If I'm reimporting objects that already exist in the store I'd ideally like to update them. Likewise, I'd like to leave any existing data that's not included in the import alone. What's the best practice in this situation? Should I assign each NSManagedObject a unique ID as and when it's created and compare this ID when determining whether an object is equal?

I'm aware that each NSManagedObject has an objectID assigned to it, but I'm not entirely sure I can rely on this given that exported data could be imported onto an entirely separate device (where objectID's could well collide or, more likely, the object would be assigned a new objectID upon creation).

4

1 回答 1

1

How do I handle the identification of existing data during import?

I would assign my own unique identifier for each item so an import/sync/export would be posible for other architectures as well (you might also need to include versioning information). This will allow identification of objects across devices.

You cannot rely on the objectID when exporting to other devices:

There is no way to create a managed object with a specific object id ( see here ), this would make it imposible to avoid assigning you own identifier to use for import/sync/export (unless you upload/download the entire DB file to the other device).

Bear in mind, Uniquing (primary key) in CoreData is not trivial and might effect performance if not done correctly.

于 2013-03-30T15:24:32.343 回答