Wiingaard
Wiingaard

Reputation: 4302

Realm migration not called

I've added a value to a realm object (I've added dynamic var inspectorName = "" to the WeekReport object), and I'm trying to migrate the realm database to contain that value. I'm trying to call the migration block in func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) like this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    print("HERE")
    Realm.Configuration.defaultConfiguration = Realm.Configuration(
        schemaVersion: 1,
        migrationBlock: { migration, oldSchemaVersion in
            if (oldSchemaVersion < 1) {
                migration.enumerateObjects(ofType: WeekReport.className()) { oldObject, newObject in
                    newObject!["inspectorName"] = ""
                }
            }
    })

    return true
}

But it seems that didFinishLaunchingWithOptions isn't called before my error happens.

In multiple view controller i have let realm = try! Realm(). Here Xcode breaks when I run the app:

"Migration is required due to the following errors: - Property 'WeekReport.inspectorName' has been added." UserInfo={NSLocalizedDescription=Migration is required due to the following errors: - Property 'WeekReport.inspectorName' has been added., Error Code=10}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.63/src/swift/

How come the migration blick isn't called? "HERE" is never printed...

Should I define realm in a different way in my view controllers?

Upvotes: 8

Views: 3513

Answers (5)

milczi
milczi

Reputation: 7572

In my case schemaVersion: 1, was to low and migration block was never called. Make sure that your new version is greater then previous.

It was my first migration but I had to change it to schemaVersion: 2 and then it started working.

Upvotes: 3

Anson Yao
Anson Yao

Reputation: 1584

It seems the problem is the migration is an async call and we can only access the Realm after the migration is completed. So the flow should be first: Set the configuration. Second: Use Realm.asyncOpen to force Realm update and hold off other things which may access try! Realm(). You can find more details here: https://realm.io/docs/swift/latest/#asynchronously-opening-realms

Upvotes: 0

kishikawa katsumi
kishikawa katsumi

Reputation: 10573

If you write let realm = try! Realm() in a view controller as an instance variable, it will be called before application: didFinishLaunchingWithOptions from Storyboard. To resolve this, you can use lazy var realm = try! Realm() instead. lazy defers creating an instance variable until the variable is accessed.

Upvotes: 8

kallahir
kallahir

Reputation: 21

In your code, try calling:

 _ = try! Realm()

before:

return true

It worked for me!

Upvotes: 0

Dmitry
Dmitry

Reputation: 7340

In multiple view controller i have let realm = try! Realm().

It seems like one of your view controllers creates Realm before application: didFinishLaunchingWithOptions, so the default configuration with migration is not set by that time.

Make sure that you configure Realm.Configuration.defaultConfiguration before any instances of Realm are created.

Upvotes: 2

Related Questions