如何从iOS完全删除Realm数据库? [英] How to completely remove Realm database from iOS?

查看:244
本文介绍了如何从iOS完全删除Realm数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在出现错误Property types for 'value' property do not match. Old type 'float', new type 'double'如何成功清除数据库或迁移?

Now I get error Property types for 'value' property do not match. Old type 'float', new type 'double' How to clear database or migrate is successfully?

推荐答案

要从磁盘上完全删除Realm文件并从头开始,只需使用NSFileManager手动删除它即可.

To completely delete the Realm file from disk and start from scratch, it's simply a matter of using NSFileManager to manually delete it.

例如,删除默认的Realm文件:

For example, to delete the default Realm file:

NSFileManager.defaultManager().removeItemAtURL(Realm.Configuration.defaultConfiguration.fileURL!)

如果要保留Realm文件,但将其完全清空对象,则可以调用deleteAll()这样做:

If you want to preserve the Realm file, but completely empty it of objects, you can call deleteAll() to do so:

let realm = try! Realm()
try! realm.write {
  realm.deleteAll()
}

更新:我觉得自己在原始答案中没有提到这一点.如果选择从磁盘删除Realm文件,则必须先在应用程序的任何线程上打开它,然后再删除它.打开后,Realm将在内部缓存对其的引用,即使文件被删除,该引用也不会被释放.

Update: I feel I neglected to mention this in my original answer. If you choose to delete the Realm file from disk, you must do so before you've opened it on any threads in your app. Once it's opened, Realm will internally cache a reference to it, which won't be released even if the file is deleted.

如果绝对需要在删除前打开Realm文件来检查其内容,则可以将其包含在autoreleasepool中.

If you absolutely do need to open the Realm file to check its contents before deletion, you can enclose it in an autoreleasepool to do this.

这篇关于如何从iOS完全删除Realm数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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