Migrate an object into a List


#1

I’ve been trying to migrate my Realm schema (swift 4.2, RealmSwift 3.1):

In the oldSchema, I have the following:

class Activity: Object {
  @objc dynamic var user: User? // this is just another Realm Object
}

In the newSchema, I’m trying to move the User into a List of User(s).

class Activity: Object {
     let users = List<User>() 
}

When performing the Realm migration, how would I move the user object from the oldSchema into the users list in the newSchema.

I’ve tried

        let config = Realm.Configuration(
            fileURL: fileURL,
            schemaVersion: 1,
            migrationBlock: { migration, oldSchemaVersion in
                if (oldSchemaVersion < 1) {
                    migration.enumerateObjects(ofType: Activity.className()) { oldObject, newObject in
                        newObject!["users"] = [oldObject!["user"]]
                    }
                }
        })
        Realm.Configuration.defaultConfiguration = config

(from: https://stackoverflow.com/questions/37488940/realm-migration-migrate-an-object-into-a-list)

But I am getting Error:
*** Terminating app due to uncaught exception ‘RLMException’, reason: ‘Object is already managed by another Realm. Use create instead to copy it into this Realm.’