Например, у меня есть эта простая схема:
class Order: Object {
@objc dynamic var id = " "
@objc dynamic var address = " "
@objc dynamic var callDate = Date(timeIntervalSince1970: 0)
}
Мне нужно выполнить миграцию, чтобы изменить свойство callDate = Date(timeIntervalSince1970: 0)
для необязательного типа callDate : Date?
, я провел некоторый тест безуспешно внутри приложения ( 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: 4,
// 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 < 4) {
// Nothing to do!
migration.enumerateObjects(ofType: Order.className()) { oldObject, newObject in
// combine name fields into a single field
let callDate = oldObject!["callDate"] as! Date?
newObject!["callDate"] = callDate
}
// 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
Ошибка консоли вывода:
Fatal error: Error initializing database: Error Domain=io.realm Code=10 "Migration is required due to the following errors:
- Property 'Order.callDate' has been made optional." UserInfo={NSLocalizedDescription=Migration is required due to the following errors:
- Property 'Order.callDate' has been made optional., Error Code=10}: file /Users/davidgranado/Documents/projects/Mobile%20Tech2Me/novacopy_ios/MobileTech/Managers/DatabaseManager.swift, line 27
Класс, выдающий ошибку:
import Foundation
import RealmSwift
class DatabaseManager {
enum UpdatePolicy {
case error, all, modified
var realmValue: Realm.UpdatePolicy {
switch self {
case .error: return .error
case .all: return .all
case .modified: return .modified
}
}
}
// The Error happens here
static let shared: DatabaseManager = {
do { return try DatabaseManager() }
catch { fatalError("Error initializing database: \(error)") }
}()
}
Любая помощь приветствуется, большое спасибо!