I've recently created a new branch and attempted refactoring much of my code to give Realm
a shot over CoreData
. But so far I haven't had any luck getting my code to run.
First, an exception is thrown in shared_realm.cpp. The line of code that throws the error is:
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 over this exception, it catches on 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 throws this error message:
fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=1 "Realm at path already opened with different schema version." UserInfo={NSLocalizedDescription=Realm at path already opened with different schema version., Error Code=1}
I am completely new to Realm, so any help is appreciated. My understanding from the documentation is that Realm()
is the proper way to access the default database, which is fine for my purposes at the moment. At first I thought that perhaps a Realm had to be passed around, but I see from online examples that this doesn't appear to be the case.
I have cleaned, changed simulators, and even updated Xcode. I also attempted to comment this line of code back in:
// FIXME - enable schema comparison
/*if (realm->config().schema != config.schema) {
throw MismatchedConfigException("Realm at path already opened with different schema");
}*/
to no avail. Feeling quite lost, so any direction is appreciated.
The schema version for a path can't be changed after it has been opened, so you'll need to change the schema before calling the path with 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
})
}
}
You might be running into this problem, because you changed the schema after you already built the app once (just a guess, you can confirm this by deleting the app and rebuilding it, which also clears the existing realm database.)
If that is indeed the problem, you should look into https://realm.io/docs/swift/latest/#migrations which outlines the recommended way to fix this issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With