Last active
August 3, 2020 15:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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