How can I easily delete all objects in a Realm

后端 未结 4 2094
轮回少年
轮回少年 2020-12-09 01:41

I have the choice of doing a migration, but I would prefer to delete everything in my defaultRealm(). How can I do this easily?

realm.deleteObject(object)
         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 02:10

    I think removing the Realm DB file is the valid answer considering that the question was about removing a whole storage rather than migrating it.

    Here is a quick Swift code for that (as of Swift 2.1 and Realm 0.96.2):

    if let path = Realm.Configuration.defaultConfiguration.path {
        try! NSFileManager().removeItemAtPath(path)
    }
    

    I use this code in the DEBUG version of the app if the migration error happens on loading the storage and then I recreate the storage. During development the schema can change a lot, so it would be too cumbersome to bother with migration all the time.

提交回复
热议问题