使用Swift删除Realm模型 [英] Delete a Realm model with Swift

查看:264
本文介绍了使用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.

这篇关于使用Swift删除Realm模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆