How to completely remove Realm database from iOS?

前端 未结 7 1358
情书的邮戳
情书的邮戳 2020-12-15 08:32

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?

相关标签:
7条回答
  • 2020-12-15 09:10

    Have you tried

       let realm = try! Realm()
       realm.deleteAllObjects() 
    

    You could also try deleting the realm off the device by plugging it into the computer, going to Xcode and then Devices, and then finding the current realm there and deleting it.

    0 讨论(0)
  • 2020-12-15 09:15

    This is how to do in swift 4.1:

    FileManager.default.removeItem(at:Realm.Configuration.defaultConfiguration.fileURL!)  
    
    0 讨论(0)
  • 2020-12-15 09:26

    Swift 4.2:

    func remove(realmURL: URL) {
            let realmURLs = [
                realmURL,
                realmURL.appendingPathExtension("lock"),
                realmURL.appendingPathExtension("note"),
                realmURL.appendingPathExtension("management"),
                ]
            for URL in realmURLs {
                try? FileManager.default.removeItem(at: URL)
            }
    let url = Realm.Configuration.defaultConfiguration.fileURL!
    remove(realmURL: url)
    
    0 讨论(0)
  • 2020-12-15 09:28

    In addition to using NSFileManager to remove the file, also remove the .lock file and .management folder as well. Otherwise, if you try to recreate the realm file with the same name, it will give an error saying it can't find it

    0 讨论(0)
  • 2020-12-15 09:30

    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.

    0 讨论(0)
  • 2020-12-15 09:30

    Swift 4.2 To remove database:

    func remove(realmURL: URL) {
            let realmURLs = [
                realmURL,
                realmURL.appendingPathExtension("lock"),
                realmURL.appendingPathExtension("note"),
                realmURL.appendingPathExtension("management"),
                ]
            for URL in realmURLs {
                try? FileManager.default.removeItem(at: URL)
            }
    let url = Realm.Configuration.defaultConfiguration.fileURL!
    remove(realmURL: url)
    

    To clear database:

    try? realm.write {
        realm.deleteAll()
    }
    
    0 讨论(0)
提交回复
热议问题