使用Swift删除Realm模型

问题描述:

我需要从Realm Cocoa数据库中删除旧的空模型.

I need to remove old, empty models from a Realm Cocoa database.

似乎有在Java中实现此目标的方法,但不在Swift中.正确吗?

There seems to be a way to do it in Java, but not in Swift. Is that correct?

如果您删除属性并启动迁移,Realm将删除表中的相应列:

If you remove a property and initiate a migration Realm will remove the corresponding column in the table:

class Dog: Object {
  dynamic var name = ""
  // dynamic var age = 0
}

但是,如果您完全删除模型定义,则迁移不会删除表:

But, if you remove the model definition entirely, the migration does not remove the table:

// class Dog: Object {
//   dynamic var name = ""
//   dynamic var age = 0
// }

这是Realm Browser的屏幕截图,显示了我要删除的空表:

Here's a screenshot from Realm Browser showing the empty tables I want to delete:

您可以调用

You can call Migration.deleteData(_:) within your migration block to specify that the named class should be completely removed from your Realm file.