在RealmSwift迁移中删除表 [英] Drop a table in a RealmSwift migration
问题描述
我正在从架构中删除旧的未使用表.如何从较早版本的应用程序中将其删除(DROP表)?到目前为止我尝试过的
I am removing an old unused table from the schema. How can I delete it (DROP Table) from older versions of the app? What I have tried so far
- 从
configration.objectTypes
中删除了 - 碰撞的模式版本
- 运行应用
- 打开了Realm Studio,桌子GameScore仍然与以前已经存在的数据一起存在
GameScore.self
添加
config.migrationBlock = { migration, oldSchemaVersion in
if oldSchemaVersion < 10 {
migration.enumerateObjects(ofType: "GameScore", { (oldObject, newObject) in
if let oldObject = oldObject {
migration.delete(oldObject)
}
})
}
}
将从GameScore中删除所有数据.我仍然不明白为什么Realm不能完全删除GameScore表(我仍然可以在Realm Studio中看到它)
Will remove all the data from GameScore. I still don't get why realm doesn't remove the GameScore table completely (I can still see it with Realm Studio)
推荐答案
删除表与从领域及其所有数据中删除领域对象相同.
Dropping a table would be the same as deleting a Realm Object from a Realm and all of it's data.
如果是这种情况,则无需进行迭代.在您的迁移块中,只需使用 deleteData(forType :)
If that's the case, there's no need for iteration. In your migration block just use deleteData(forType:)
删除具有给定名称的类的数据.
Deletes the data for the class with the given name.
给定类的所有对象将被删除.如果Object子类您的程序中不再存在该类的任何剩余元数据将从Realm文件中删除.
All objects of the given class will be deleted. If the Object subclass no longer exists in your program, any remaining metadata for the class will be removed from the Realm file.
三个步骤
1)从代码中删除领域对象类
1) Remove the Realm Object class from your code
2)在迁移块中增加schemaVersion.这会通知领域存在新模式
2) Increment the schemaVersion in your migration block. This notifies realm there's a new schema
3)使用名称字符串删除迁移块中的对象及其数据.
3) Delete the object and it's data within the migration block by using a string of it's name.
假设我们正在处理模式版本1,并且有一个我们想要摆脱的TestClass Realm对象.从您的代码中删除该类,增加到模式2并使用deleteData
Suppose we are working on schema version 1 and have a TestClass Realm object we want to get rid of. Remove the class from your code, increment to schema 2 and use deleteData
let config = Realm.Configuration (
schemaVersion: 2,
migrationBlock: { migration, oldSchemaVersion in
migration.deleteData(forType: "TestClass")
})
这篇关于在RealmSwift迁移中删除表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!