Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
893 views
in Technique[技术] by (71.8m points)

swift - How to completely remove Realm database from iOS?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

For example, to delete the default Realm file:

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

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()
}

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...