Swift中的领域迁移
我有一个领域对象建模为这样Swift中的领域迁移,swift,migration,realm,data-migration,data-management,Swift,Migration,Realm,Data Migration,Data Management,我有一个领域对象建模为这样 class WorkoutSet: Object { // Schema 0 dynamic var exerciseName: String = "" dynamic var reps: Int = 0 // Schema 0 + 1 dynamic var setCount: Int = 0 } 我正在尝试执行迁移 在我的AppDelegate中,我导入了RealmSwift 在函数中,didfishlaunch
class WorkoutSet: Object {
// Schema 0
dynamic var exerciseName: String = ""
dynamic var reps: Int = 0
// Schema 0 + 1
dynamic var setCount: Int = 0
}
我正在尝试执行迁移
在我的AppDelegate
中,我导入了RealmSwift
在函数中,didfishlaunchwithoptions
I调用
Migrations().checkSchema()
迁移是在另一个文件中声明的类
在该文件中有一个声明为的结构
func checkSchema() {
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
switch oldSchemaVersion {
case 1:
break
default:
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
self.zeroToOne(migration)
}
})
}
func zeroToOne(migration: Migration) {
migration.enumerate(WorkoutSet.className()) {
oldObject, newObject in
let setCount = 1
newObject!["setCount"] = setCount
}
}
将
setCount
添加到模型中时出现错误您需要调用迁移。仅仅创建一个配置,不会调用它。有两种方法可以做到这一点:
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
if oldSchemaVersion < 1 {
migration.enumerate(WorkoutSet.className()) { oldObject, newObject in
newObject?["setCount"] = setCount
}
}
}
)
Realm.Configuration.defaultConfiguration = config
let config=Realm.Configuration(
//设置新架构版本。该版本必须大于以前使用的版本
//版本(如果以前从未设置过架构版本,则版本为0)。
阴谋厌恶:1,
//设置使用打开域时自动调用的块
//低于上面设置的架构版本
migrationBlock:{migration,oldSchemaVersion in
如果oldschemaversation<1{
migration.enumerate(WorkoutSet.className()){oldObject,newObject在
新建对象?[“设置计数”]=设置计数
}
}
}
)
Realm.Configuration.defaultConfiguration=config
现在,您的迁移应该可以正常工作了 因为您刚刚创建了
领域配置
。如果需要,领域将调用迁移块。您不能直接调用迁移
所以,要调用迁移块,您应该将配置对象设置为Realm,或者设置为默认配置。然后创建领域实例
因此,您需要执行以下操作:
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
switch oldSchemaVersion {
case 1:
break
default:
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
self.zeroToOne(migration)
}
})
let realm = try! Realm(configuration: config) // Invoke migration block if needed
你有什么错误?您可以共享吗?属性“setCount”已添加到最新模型别忘了调用import realmswift不要调用
let realm=try!领域(配置:config)
强制。它被标记为抛出是有原因的(例如,设备上没有空间执行迁移)。相反,您应该在do catch块中调用它。是否有一种方法可以自动执行此操作?我认为这是相当直接的,如果值可以是零。。。有什么想法吗?