Krekin
Krekin

Reputation: 1560

Why is new AppStore build crashing when installed on top of older version after Realm was updated?

I've been using Realm for more than a year on a specific app & it has been in the AppStore for just as long. Everything has been fine until a few days ago when a newly approved & released build into the AppStore now crashes for ALL users after updating & attempting to open the app.

Users are saying the only way to fix this is by deleting the app from the device & doing a fresh install. That's a catastrophe. Luckily we save user data/progress on a server.

This started happening immediately after Realm was updated by using the pod update command in terminal.

By installing a previous build from TestFlight, opening it at least once & then building & running the app on the device with the old TestFlight build from Xcode produces a very specific crash error in the log when initializing an instance of Realm:

"Opening Realm files of format version 20 is not supported by this version of Realm"

Note, I did NOT touch or alter the code in any way after pods were updated.

Here's how the Realm configuration currently looks like (& has looked like for the past 6 months):

let config = Realm.Configuration(schemaVersion: 30, migrationBlock: { migration, oldSchemaVersion in
    if oldSchemaVersion < 30 {
        migration.deleteData(forType: CategoryRealmModel.className())
        migration.deleteData(forType: CourseRealmModel.className())
    }
})
Realm.Configuration.defaultConfiguration = config

Would really appreciate any ideas on what can be done. I need to release a new build that will not crash when updated on top of any older versions.

Upvotes: 1

Views: 239

Answers (1)

Krekin
Krekin

Reputation: 1560

Eureka, I found the answer. To be able to overlay a new build on top of an older one without having Realm crash, requires the alteration of the Realm configuration as such:

let config = Realm.Configuration(schemaVersion: 30, migrationBlock: { migration, oldSchemaVersion in
    if oldSchemaVersion < 30 {
        migration.deleteData(forType: CategoryRealmModel.className())
        migration.deleteData(forType: CourseRealmModel.className())
    }
}, deleteRealmIfMigrationNeeded: true)
Realm.Configuration.defaultConfiguration = config

It's not enough to merely use the function migration.deleteData(forType: SomeRealmModel.className()) on every existing RealmModel within the app. One needs to set deleteRealmIfMigrationNeeded to true during configuration.

Apparently it triggers a better inner process - full deletion of the existing Realm database if it deems migration necessary. Which it will if an older version of the app exists.

Upvotes: 1

Related Questions