Realm's setDefaultRealmSchemaVersion isn't executed

137 Views Asked by At

I'm trying to migrate a Realm and assign a property to be a primary key - but setDefaultRealmSchemaVersion seems like its not being executed:

func applicationDidFinishLaunching(aNotification: NSNotification) {

    // prints "Current Version: 3"
    println("Current version \(schemaVersionAtPath(Realm.defaultPath, encryptionKey: nil, error: nil)!)")

    let migrationBlock: MigrationBlock = { migration, oldSchemaVersion in

        if oldSchemaVersion < 4 {

            migration.enumerate(Fruits.className()) {oldObject, newObject in
                    let id = oldObject!["id"] as! Int
                    newObject!["primaryKeyProperty"] = id
            }
        }
        println("Migration complete")
    }

    setDefaultRealmSchemaVersion(4, migrationBlock)

    // prints "New Version: 3"
    println("New Version: \(schemaVersionAtPath(Realm.defaultPath, encryptionKey: nil, error: nil)!)")  
}

Any ideas why is that? Thanks in advance!

1

There are 1 best solutions below

2
On

Your Realm's migration block will only be executed if you explicitly migrate the Realm, or create the Realm via Realm().