core data copying items with relationships

Discussion in 'Mac Programming' started by Chirone, Aug 25, 2009.

  1. Chirone macrumors 6502

    Mar 2, 2009
    following the apple tutorial about core data ( i tried to naively extend this to copy the relationship by adding the key of the relationship to the keysToBeCopied array

    the output window told me i have to implement an encodeWithCoder method
    so i did.
    it looked something a little like this:
    - (void)encodeWithCoder:(NSCoder *)c {
    	[c encodeObject:self.title forKey:@"title"];
    - (id) initWithCoder: (NSCoder *)c {
    	self = [super init];
    	if(self) {
    		self.title = [c decodeObjectForKey:@"title"];
    	return self;
    it then complained saying it couldn't find the method setTitle.. so i changed title from dynamic to synthesize and made an instance variable
    then it complained at me again saying i was making an illegal attempt to establish relationships between different contexts

    any idea how this is done? This is a to-one relationship that i was trying to copy but i'll also want to copy for to-many relationships

    should i just add the first object to the pasteboard and when it gets pasted just make copies of the objects it's related to? is that efficient?
  2. MrFusion macrumors 6502a

    Jun 8, 2005
    If you have to copy between contexts (e.g. different documents), then as far as I know you have no other option than copying the individual objects and re-establishing the relationships. Just copying it won't work, I am afraid.

Share This Page