Selman ALPDÜNDAR

Solution of Realm Migration Error Code 10

When you make a change on model you have two options. You can delete app from simulator and there will not a problem after running again but if you already has shipped your app
to apple store this option is not acceptable for you. The other option is using migration.
This code is a quotation from realm.io
Suppose we have the following Person model:

class Person: Object {
    @objc dynamic var firstName = ""
    @objc dynamic var lastName = ""
    @objc dynamic var age = 0
}

We want to update the data model to require a fullName property, rather than separate first and last names. To do this, we simply change the object interface to the following:

class Person: Object {
    @objc dynamic var fullName = ""
    @objc dynamic var age = 0
}

At this point if you had saved any data with the previous model version, there will be a mismatch between what Realm sees defined in code and the data Realm sees on disk. When this occurs, an exception will be thrown when you try to open the existing file unless you run a migration.

Local migrations
Local migrations are defined by setting Realm.Configuration.schemaVersion and Realm.Configuration.migrationBlock. Your migration block provides all the logic for converting data models from previous schemas to the new schema. When creating a Realm with this configuration, the migration block will be applied to update the Realm to the given schema version if a migration is needed.

Suppose we want to migrate the Person model declared earlier. The minimal necessary migration block would be the following:

// Inside your application(application:didFinishLaunchingWithOptions:)

let config = Realm.Configuration(
    // Set the new schema version. This must be greater than the previously used
    // version (if you've never set a schema version before, the version is 0).
    schemaVersion: 1,

    // Set the block which will be called automatically when opening a Realm with
    // a schema version lower than the one set above
    migrationBlock: { migration, oldSchemaVersion in
        // We haven’t migrated anything yet, so oldSchemaVersion == 0
        if (oldSchemaVersion < 1) {
            // Nothing to do!
            // Realm will automatically detect new properties and removed properties
            // And will update the schema on disk automatically
        }
    })

// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config

// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
let realm = try! Realm()

At the very minimum we need to update the version with an empty block to indicate that the schema has been upgraded (automatically) by Realm.

The problem
When you start your app on simulator you may get error that said “fatal error: ‘try!’ expression unexpectedly raised an error: Error Domain=io.realm Code=10 “Migration is required due to the following errors:”. This happens because in some view controller you are trying initialize before migration.

The Solution
You need to make the initialize lazy then the controller will wait until migration is done.

    lazy var realm:Realm = {
        return try! Realm()
    }()


5 comments

  1. Udemy projesi yaptığım için ilk paragraf yeterli oldu ama siteyi bookmarklıyorum. Çok faydalı görünüyor. Çok teşekkürler.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.