RealmSwift: Realm along a path already open with a different version of the scheme

I recently created a new branch and tried to reorganize most of my code to give Realm snapshot of CoreData . But so far I have been unlucky for my code to work.

First, an exception is thrown in shared_realm.cpp. The line of code that causes the error:

 if (realm->config().schema_version != config.schema_version && config.schema_version != ObjectStore::NotVersioned) { throw MismatchedConfigException("Realm at path already opened with different schema version."); } 

If I skip this exception, it will catch the second line of code in the following:

 class func getAllCategories() -> Results<Category> { let realm = try! Realm() let categories = realm.objects(Category) return categories } 

And it gives this error message:

fatal error: 'try!' the expression unexpectedly caused an error: Error Domain = io.realm Code = 1 "The kingdom is on a path already open with different version of the scheme." UserInfo = {NSLocalizedDescription = Realm on the path is already open with a different version of the schema., Error code = 1}

I am completely new to the kingdom, so any help is appreciated. My understanding from the documentation is that Realm() is the right way to access the default database, which I can currently use. At first I thought it might have been necessary to survive the kingdom, but from the online examples I see that this does not seem to be the case.

I cleaned, changed simulators, and even updated Xcode. I also tried to comment on this line of code:

 // FIXME - enable schema comparison /*if (realm->config().schema != config.schema) { throw MismatchedConfigException("Realm at path already opened with different schema"); }*/ 

to no avail. Feeling completely lost, so any direction is appreciated.

+6
source share
2 answers

The schema version for the path cannot be changed after it is opened, so you will need to change the schema before calling the path using setSchemaVersion .

 setSchemaVersion(1, realmPath: Realm.defaultPath) { (migration, oldSchemaVersion) -> Void in if oldSchemaVersion < 1 { migration.enumerate(Category.className(), { (oldObject, newObject) -> Void in let constant = oldObject!["constant"] as! String newObject!["constant"] = constant }) } } 
+1
source

You may have encountered this problem because you changed the schema after you already created the application once (just guess, you can confirm this by deleting the application and rebuilding it, which will also clear the existing area database.)

If this is really a problem, you should study the https://realm.io/docs/swift/latest/#migrations which outlines the recommended way to fix this problem.

+1
source

All Articles