I am in the process of migrating a few classes from using objects and relations to embedded objects. There is a lot of edge cases when converting doing this migration. I need to ensure that there is exactly one backlink for each object that will be converted otherwise the app will crash at startup.
After a lot of work, I have gotten the first case to work. I can successfully delete all orphaned objects. Now I need to handle the second case. Whenever there is an object that has multiple backlinks I want to keep the first backlink and for all subsequent backlinks I want to create a new object and copy all values over to the new object, which brings me to the question.
How can I create embedded objects during migration?
Here are two approaches I have tried
Option 1: Create an unattached object by value and replace the object in the second backlink
/** This will crash with error Object cannot be inserted unless the schema is initialized. This can happen if you try to insert objects into a RLMArray / List from a default value or from an overriden unmanaged initializer (`init()`) **/ let listOfObjects = secondBacklinkObject?.dynamicList("linksToFirstObject") let copy = DynamicObject(value: objectToCopyCopy) listOfObjects?.replace(index:indexOfChildWithMultipleBacklinks, object: copy)
Option 2: Create a managed object and replace it in the second backlink
/** This will crash with error: Embedded objects cannot be created directly */ let listOfObjects = secondBacklinkObject?.dynamicList("linksToFirstObject") let copy = migration.create("EmbeddedObject", value: objectToCopy) listOfObjects?.replace(index:indexOfChildWithMultipleBacklinks, object: copy)
Is there an api for creating an embedded object during migration? If not, how can I prevent data loss in cases where there are multiple backlinks when converting “object” to embeddedobject? The example is in swift, but I will have the same question for Kotlin as well as the app is available on both platforms.