Clear complete Realm Database

后端 未结 6 2040
半阙折子戏
半阙折子戏 2020-12-24 11:45

I\'m playing around with realm (currently 0.85.0) and my application uses the database to store user-specific data such as the contacts of the current user. When the user de

6条回答
  •  时光取名叫无心
    2020-12-24 11:52

    RealmSwift: Simple reset using a flag

    Tried the above answers, but posting one more simple way to delete the realm file instead of migrating:

    Realm.Configuration.defaultConfiguration.deleteRealmIfMigrationNeeded = true
    

    This simply sets a flag so that Realm can delete the existing file rather than crash on try! Realm()

    Instead of manually deleting the file

    Thought that was simpler than the Swift version of the answer above:

    guard let path = Realm.Configuration.defaultConfiguration.fileURL?.absoluteString else {
        fatalError("no realm path")
    }
    
    do {
        try NSFileManager().removeItemAtPath(path)
    } catch {
        fatalError("couldn't remove at path")
    }
    

提交回复
热议问题