前幾天在給Realm數(shù)據(jù)庫的一個(gè)表加字段的時(shí)候,控制臺(tái)報(bào)了一個(gè)錯(cuò).Migration is required due to the following errors
網(wǎng)上Google,然后看了一下官方文檔.
//在Appdelegate加上這個(gè)代碼段就行了.
// Inside your application(application:didFinishLaunchingWithOptions:)
let config = Realm.Configuration(
? ? // Set the new schema version. This must be greater than the previously used
? ? // version (if you've never set a schema version before, the version is 0).
? ? schemaVersion: 1,
? ? // Set the block which will be called automatically when opening a Realm with
? ? // a schema version lower than the one set above
? ? migrationBlock: { migration, oldSchemaVersion in
? ? ? ? // We haven’t migrated anything yet, so oldSchemaVersion == 0
? ? ? ? if (oldSchemaVersion < 1) {
? ? ? ? ? ? // Nothing to do!
? ? ? ? ? ? // Realm will automatically detect new properties and removed properties
? ? ? ? ? ? // And will update the schema on disk automatically
? ? ? ? }
? ? })
// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config
// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
let realm = try! Realm()
可能碰到其他需求,可以參考文檔.
:https://realm.io/docs/swift/latest/#migrations