Re: Copying objects between CoreData stores
Re: Copying objects between CoreData stores
- Subject: Re: Copying objects between CoreData stores
- From: Chris Hanson <email@hidden>
- Date: Wed, 04 Jan 2012 11:42:58 -0800
On Jan 3, 2012, at 11:32 PM, Rick Mann wrote:
> I'm working on a Core Data-based CAD app. The schema includes Parts and PartInstances. There is a to-many relationship from Part to PartInstance. The Library contains Parts, but a CAD file contains Parts and PartInstances.
>
> When the user creates a new PartInstance in a document, I need to copy the Part object from the Library to the document. This is not just a copy from one NSManagedObjectContext to another, it's also a copy from one peristent store to another. That's fine.
>
> But when the user instantiates a new PartInstance of the same Part again, this time I don't want to copy the Part over, because it already exists. I need a way to determine if the Part object already exists in the document's MOC.
>
> I can create a UUID of sorts to use, but I'm wondering if I can do something to create the new object (in the new persistent store) with the same objectID as the object from which it's coming. That makes it much easier to then check for the existence of that object in the new store/context before creating it. Note that I have several other entities in the overall schema, and it would be nice not to have to add a uuid to each of those, as well.
There's no way to create an object with the same managedObjectID as an existing object in Core Data, by design: Object IDs are really intended to be unique.
An alternate approach might be to have a Part contain two additional attributes: Your own unique ID (such as a UUID), and a "based-on Part" attribute that can contain the original Part's unique ID.
So for example, when you put a Part from the library in a document, what really happens is:
1. Create a new Part in the document with the same base attributes as its original Part.
2. Set the new Part's unique ID to a new UUID. (This could be part of #1, e.g. in awakeFromInsert.)
3. Set the new Part's based-on Part to the library Part's unique ID.
4. Create a new PartInstance in the document referring to the Part in the document.
This way a Part in a document can be linked back to its original Part in the library, but documents remain self-contained.
For performance, you'll probably want to create an index on both the unique ID and the based-on Part ID.
If you add a last-modified-date or generation-number to Part, you can use that to detect when you need to propagate changes from the Part in the library to the Part in a document.
-- Chris
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden