andyl
andyl

Reputation: 83

Realm Migration: Migrate an object into a List

So, I've been trying to migrate my Realm schema, but I can't seem to do the following.

In the oldSchema, I have the following:

class Period: Object {
  dynamic var weekday: Weekday! // this is just another Realm Object
}

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

class Period: Object {
  let weekdays: List<Weekday> = List<Weekday>()
}

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

Thanks.

Upvotes: 3

Views: 934

Answers (1)

Jans
Jans

Reputation: 11250

You can run a migration block under Realm Configuration.

Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 2,
migrationBlock: { migration, oldSchemaVersion in

    migration.enumerate(Period.className()) { oldObject, newObject in
        // filter the versions where this change would take place
        // if oldSchemaVersion < 1 {
        // }...
        newObject["weekdays"] = [oldObject["weekday"]]              
   })

Upvotes: 2

Related Questions