Skip to content

Instantly share code, notes, and snippets.

@IvanovDeveloper
Last active August 3, 2020 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IvanovDeveloper/c0471490c60ffe029156381acefc8032 to your computer and use it in GitHub Desktop.
Save IvanovDeveloper/c0471490c60ffe029156381acefc8032 to your computer and use it in GitHub Desktop.
Realm Database initialisation in the app with migration issue from 4.4.0 -> 5.3.0
class Database: NSObject {
@objc static let shared = Database()
private let schemaVersion: UInt64 = 1 // In the prod version this value was incremented.
static var realm: Realm? {
do {
let realm = try Realm()
return realm
} catch {
print("Realm could not access database: ", error)
return nil
}
}
func setup() {
Realm.Configuration.defaultConfiguration = Realm.Configuration(schemaVersion: schemaVersion, migrationBlock: { [weak self] migration, oldSchemaVersion in
guard let `self` = self else {return}
// We haven’t migrated anything yet, so oldSchemaVersion == 0
if (oldSchemaVersion < self.schemaVersion) {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment