Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 289
Wiingaard Avatar asked Jan 18 '17 19:01

Wiingaard


3 Answers

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.

like image 129
kishikawa katsumi Avatar answered Nov 01 '22 12:11

kishikawa katsumi


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.

like image 21
milczi Avatar answered Nov 01 '22 12:11

milczi


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.

like image 2
Dmitry Avatar answered Nov 01 '22 12:11

Dmitry