iosswiftrealmrealm-migration

Realm Migration Error: 'RLMException': 'Property 'RecipeStep.recipeId' does not exist'


I'm running into issues during a Realm migration. The error messages says that the property doesn't exist, but this is what my new object looks like:

class RecipeStep: Object {
   @objc dynamic var recipeId: Int = 0
   let stepNumber = RealmOptional<Int>()
   @objc dynamic var stepText: String? = nil
}

And here's what the old object schema looks like:

class RecipeStep: Object {
   let recipeId = RealmOptional<Int>()
   let stepNumber = RealmOptional<Int>()
   @objc dynamic var stepText: String? = nil
}

As you can see, the single change is the type of the recipeId: converting RealmOptional to an Int. And here's the migration block I'm using to do it:

migration.enumerateObjects(ofType: RecipeStep.className()) { oldObject, newObject in
               if let recipeStepRecipeId = oldObject?["recipeId"] as? Int {
                  newObject?["recipeId"] = recipeStepRecipeId
               }
            }

What am I doing wrong?


Solution

  • This turns out to have been a bug in an older version of Realm (v5.4.5). I updated to version 10.0.0 and this issue was resolved. The migration successfully completes. I changed nothing in my code.

    I'm on Xcode 12, building for iOS 13.

    Found discussion on a similar sounding bug on this SO thread.