Re: Core Data: Copying and pasting chained entities
Re: Core Data: Copying and pasting chained entities
- Subject: Re: Core Data: Copying and pasting chained entities
- From: Ben Trumbull <email@hidden>
- Date: Mon, 6 Oct 2008 19:41:46 -0700
Renaud,
Now, I am nearly sure that I cannot use the NSArchiver/NSUnarchiver
with
NSManagedObjects, because NSArchiver/NSUnarchiver know nothing about
Core
Data relationships and because NSUnarchiver won't know how to insert
an
object in the managed object context.
That's correct. Managed objects do not conform to NSCoding.
However, a similar mechanism must exist, because Core Data stores and
fetches entities.
Not in a fashion that's meaningful to copy&paste. The atomic stores
(xml, binary) write everything (e.g. whole file) out all the time.
The incremental sqlite store only saves deltas. Neither is helpful
for the pasteboard.
So once again: how do you implement copy and paste with Core Data ?
It is a
common operation; I was very surprised not to find any article on the
subject.
There are a few examples floating around. Typically, you need the
NSManagedObjectID and a snapshot of the attributes. Both KVC and
NSManagedObject have methods to grab a bunch of keys at once to
simplify this.
Relationships are an issue, because there is no one answer. Do you
want a shallow copy or a deep copy ? How deep ? The related objects'
related objects ? Most people settle for a mix of references and
copies.
Typically, relationships are stored externally as either references
(NSManagedObjectID in URI form) or copies (e.g. all their
attributes). Just how far you copy the object graph is up to you.
Some apps don't copy the graph at all, and just put a reference (URI)
on the pasteboard and when it's pasted back, look up the object.
- Ben
_______________________________________________
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