Mike.Zaki
Mike.Zaki

Reputation: 101

Realm Migration Fail with Error : Migration Required, or object has already been opened with a different schema version

here is the code for the migration (in didFinishLaunchingWithOptions){

    Realm.Configuration.defaultConfiguration = Realm.Configuration(
        schemaVersion: 3,
        migrationBlock: { migration, oldSchemaVersion in
            // The enumerateObjects:block: method iterates
            // over every 'Person' object stored in the Realm file
            migration.enumerate(User.className()) { oldObject, newObject in

                if oldSchemaVersion < 1 {
                    newObject!["crashTest"] = ""
                }
                if oldSchemaVersion < 2 {

                }
            }
    }) let realm = try!Realm()

Here is the error:

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=0 "Migration is required due to the following errors: - Property 'crashTest' has been added to latest object model." UserInfo={NSLocalizedDescription=Migration is required due to the following errors: - Property 'crashTest' has been added to latest object model.}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.1.101.15/src/swift/stdlib/public/core/ErrorType.swift, line 50

Upvotes: 2

Views: 2457

Answers (1)

Lorrain.T
Lorrain.T

Reputation: 11

If you are in local development only, I suggest you to reset your realm database instead do a migration. You can reset the database by deleting the app on your simulator or devices. Alternatively you can try to use NSFileManager to delete the realm file before accessing the database. let defaultPath = Realm.Configuration.defaultConfiguration.fileURL?.path try! FileManager.default.removeItem(atPath: defaultPath!)

Upvotes: 1

Related Questions