Duplicating a NSManagedObject ?

Discussion in 'Mac Programming' started by MacDonaldsd, Jan 19, 2008.

  1. MacDonaldsd macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #1
    Hi,

    Is there anyway to duplicate an NSManagedObject ? using copy does not seem to work for me.

    What I am trying to do, is to make my own organising application, and some events are repeating, and there for I just want to make a copy of an event and change the date.
     
  2. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    It isn't built in, but I have created code to do it for D&D Manager (see my sig), it would probably need some custom modification as some code is application specific tweaking (to prevent copying of some attached objects). You can view the source here.

    It is licensed under the GPL v2.0 however, so may not be suitable ;).

    Header

    Implementation.

    EDIT: If its suitable send me a PM and I can try and send you a copy of the source without the application specific stuff.
     
  3. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #3
    Oh that is annoying that it is not built in.

    Thanks for the code its appreciated, you seem to cover a lot of interesting topics in D&D. :D

    Do I need to look at all the methods, or is the duplicate one sufficient on its own ?
     
  4. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #4
    Basically my code is probably more complex than required for what you want to do. Rather than just allowing straight duplication my code also allows importing of objects from other files, including merging objects from another file over ones in the current file, which adds complexity.

    My code also has a master duplication list, which is stored in MyDocument.m which sees which files have already been duplicated, which is due to the fact that my model has loops, e.g. Character<==>Feat<==>Spell Class<==>Character, so that would cause infinite loops otherwise.

    I have updated the documentation for ERNSManagedObjectExtended so it should be more obvious what does what. There is some DOxygen documentation in the html folder in the full source which is available here.

    Most stuff is in ERNSManagedObjectExtended but if you have loops in your model graph you'll also need to look at MyDocument.m specifically the methods:

    Code:
    -(int)indexInOrigObjectsOfTheObject:(NSManagedObject *)obj;
    -(NSManagedObject *)origObjectAtIndex:(int)index;
    -(NSManagedObject *)newObjectAtIndex:(int)index;
    -(void)addOrigObject:(NSManagedObject *)obj;
    -(void)addNewObject:(NSManagedObject *)obj;
    
    And you also need to remember to initialise the arrays origObjectsCopied and newObjectsCopied which I do at initialisation of MyDocument.

    If you want to merge objects and check whether two objects are the same you'll also need to look at some methods in MenuController, but they have a subtle bug which I'll be fixing (it is mentioned in the comments in ERNSManagedObjectExtended, but I'm too busy to fix it today)
     

Share This Page