Can NSManagedObject conform to NSCoding

后端 未结 3 529
心在旅途
心在旅途 2021-01-05 16:20

I need to transfer a single object across device. Right now I am converting my NSManagedObject to a dictionary , archiving it and sending as NSData. Upon receiving I am unar

3条回答
  •  情话喂你
    2021-01-05 16:50

    The problem is obviously the unarchiver. In the end there is no way to use both initWithEntity: and initWithCoder: in the same object. However, I suspect that with some trickery you may be able to make this work. For instance, implement initWithCoder: as you have done, and in that create another managed object with initWithEntity: (this means you will need unmanaged ivars that can hold such a reference. Implement forwardingTargetForSelector:, and if the object is the one being created using initWithCoder:, forward it to the shadow object you created with initWithEntity: (otherwise, forward that selector to super). When the object is decoded fully, then ask it for the real managed object, and you're done.

    NOTE: I have not done this but have had great success with forwardingTargetForSelector:.

提交回复
热议问题