Learn Courses My Dashboard

Realm migration for app updates

Hi team,

I have an issue with migration of the realm database I would appreciate some guidance on.

My app is live in the App Store and I have implemented some new functionality that requires migration of the database on previous installations (everything runs as desired on a fresh installation).

I remember that when Chris ran into something similar on the tutorials, he just deleted the app from the device but this obviously not an option for my existing user base (as they would be deleting their user activity, which is out of the question).

I followed the documentation could find and changed the configuration to include a migration block (I’m not adding any database fields, so the guidance seems to be to not do anything within ‘if oldSchemaVersion < 1’:

static let placesConfig = Realm.Configuration(fileURL: Bundle.main.url(forResource: "Place", withExtension: "realm"), inMemoryIdentifier: nil, syncConfiguration: nil, encryptionKey: nil, readOnly: true, schemaVersion: 1, migrationBlock: { migration, oldSchemaVersion in
            if oldSchemaVersion < 1 {
        }, deleteRealmIfMigrationNeeded: false, shouldCompactOnLaunch: nil, objectTypes: nil)

I then set Realm.Configuration.defaultConfiguration and start Realm() as before.

However, this results in the following error/crash on start-up:

Fatal error: ‘try!’ expression unexpectedly raised an error: Error Domain=io.realm Code=1 “Provided schema version 1 is less than last set version 0.” UserInfo={NSLocalizedDescription=Provided schema version 1 is less than last set version 0., Error Code=1}: file Singing_Streets/PlaceService.swift, line 77

Looking online for pointers on this very odd message, I tried setting the readOnly property in the realm configuration to false.

This seemed to work on the simulator. However, when trying on my device, with the older version of the app installed, it crashed on start-up with the following error:

Unable to open a realm at path make_dir() failed: Operation not permitted Path:Exception backtrace

This last error does look like it was a direct result of not having the database as readOnly, and indeed anyone running into that online indicates as much.

Thus, I’m at something of an impasse, which is preventing me from rolling out any updates to my app.

Has anyone successfully gone through a realm database migration? Does Chris has a tutorial on this, perhaps with the Guidebook app?



Some additional details.

I also have the migration code in AppDelegate.swift to make sure that the data is migrated before anything else:

let config = Realm.Configuration(
readOnly: false, schemaVersion: 1, migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {/* Nothing to do!*/}

Realm.Configuration.defaultConfiguration = config

This executes okay and I’ve confirmed through print statements of schemaVersionAtURL(configCheck.fileURL!) that the schema version that gets picked up is the updated one (“1”)

Thus, it would appear that there’s something about the realm file it doesn’t like? It’s a simple enough file generated from a .csv file using MongoDB Realm Studio Version 10.1.1, which I have no problems with otherwise.

Looks like I only needed the entry in AppDelegate.swift.

Once I removed the removed the settings in my Constants file referring to the Place.realm, and reverted back to the original schema values, this then appears to work.

Every day is a school day.