Member-only story
“Provided schema version 0 is less than last set version 2.” Realm error swift — iOS (Migration solution)
Sometimes while you are using realm and want to change or modify the schema of database realm gives such kind of error and literally developer sucks to find the solution for it.
Here the best solution for version checking and migrate old schema version to new one without delete the database file or app.
First,open AppDelegate file import the realm libraries like:
import Realm
import RealmSwift
Now simply write following code on didFinishLaunchingWithOptions method of AppDelegate file:
let config = Realm.Configuration(
schemaVersion: 2,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 2) {
// Nothing to do!
}
})
Realm.Configuration.defaultConfiguration = config
// let configCheck = Realm.Configuration();
// let configCheck2 = Realm.Configuration.defaultConfiguration;
// let schemaVersion = configCheck.schemaVersion
// print("Schema version \(schemaVersion) and configCheck2 \(configCheck2.schemaVersion)") let configCheck = Realm.Configuration();
do {
let fileUrlIs = try schemaVersionAtURL(configCheck.fileURL!)
print("schema version \(fileUrlIs)")
} catch {
print(error)
}
For more about iOS development : https://medium.com/@javedmultani16
Some useful codes and libraries: https://github.com/javedmultani16
Know more about me: https://www.linkedin.com/in/javedmultani16/