领域主键迁移
Realm primary key migration
我想将我的领域架构迁移到新版本。因此需要删除我的主键。
旧架构:
class StudyState : Object
{
dynamic var name = ""
dynamic var x = ""
dynamic var y = ""
override static func primaryKey() -> String? {
return "name"
}
}
新架构:
class StudyState : Object
{
dynamic var name = ""
dynamic var x = ""
dynamic var y = ""
}
如果不迁移,领域将失败
'RLMException', reason: 'Migration is required for object type 'StudyState' due to the following errors:
- Property 'name' is no longer a primary key.'
我尝试了这个迁移块,但也失败了:
migration.enumerate(StudyState.className()) { oldObject, newObject in
newObject?["deleted"] = false
newObject?["primaryKeyProperty"] = ""
}
'RLMException', reason: 'Invalid property name'
有没有办法在将领域迁移到新架构版本时删除主键?
如果只删除主键注解,则无需在迁移块中执行任何操作。
但是因为schema定义改变了,所以需要增加schema版本。
如下所示:
// You have to migrate Realm BEFORE open Realm if you changed schema definitions
setSchemaVersion(1, Realm.defaultPath) { (migration, oldSchemaVersion) -> Void in
if oldSchemaVersion < 1 {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
}
let realm = Realm()
...
我想将我的领域架构迁移到新版本。因此需要删除我的主键。
旧架构:
class StudyState : Object
{
dynamic var name = ""
dynamic var x = ""
dynamic var y = ""
override static func primaryKey() -> String? {
return "name"
}
}
新架构:
class StudyState : Object
{
dynamic var name = ""
dynamic var x = ""
dynamic var y = ""
}
如果不迁移,领域将失败
'RLMException', reason: 'Migration is required for object type 'StudyState' due to the following errors: - Property 'name' is no longer a primary key.'
我尝试了这个迁移块,但也失败了:
migration.enumerate(StudyState.className()) { oldObject, newObject in
newObject?["deleted"] = false
newObject?["primaryKeyProperty"] = ""
}
'RLMException', reason: 'Invalid property name'
有没有办法在将领域迁移到新架构版本时删除主键?
如果只删除主键注解,则无需在迁移块中执行任何操作。 但是因为schema定义改变了,所以需要增加schema版本。
如下所示:
// You have to migrate Realm BEFORE open Realm if you changed schema definitions
setSchemaVersion(1, Realm.defaultPath) { (migration, oldSchemaVersion) -> Void in
if oldSchemaVersion < 1 {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
}
let realm = Realm()
...