jhc jhc - 1 year ago 208
Swift Question

RealmSwift: Detach an object from Realm, including its properties of List type

I want to create a duplicate of a persisted object such that the new instance has all the same values but is not attached to Realm. Using

Object(value: persistedInstance)
works great for classes whose properties are all strings, dates, numbers, etc. However, when duplicating an instance of a class with List type properties, the duplicate's List and the List's elements continue to reference the persisted records. How can I create a duplicate that's fully detached from Realm, including any Lists and elements in those Lists?

Answer Source

This is not yet supported natively by Realm, but a requested feature tracked by issue #3381.

For now, you would need to implement your own deep copy constructor. A common strategy is to do that on every model and call the deep copy constructors of related objects. You need to pay attention though that you don't run into cycles.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download