CoreData - Duplicate existing object

后端 未结 3 1601
执笔经年
执笔经年 2021-02-11 06:16

Hi would like to duplicate an object from my core data db. Right now I\'m using

        movement2 = [NSEntityDescription
                                     ins         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-11 06:41

    There's no method in the API to copy NSManagedObjects, since they don't conform to NSCopying, nor to NSCoding. This makes sense considering that it's often not clear how you would want to copy such an object if it has relationships: should it have relations to the same objects, or copies of them? What about those copies? These are difficult questions whose answers probably depend on the nature of the relationships (to-many or to-one), and hence on the specific use cases of Core Data.

    So short answer: I'd do what you're doing in your snippet, or maybe write a method to make a copy. It looks like your NSManagedObjects only have simple attributes and no relationships, so it should be straightforward.

提交回复
热议问题