way to purge all but one object types (models) in a realm

和自甴很熟 提交于 2019-12-01 10:56:50

问题


I want to realm.delete() all but one model in my realm. Is there any way to do this without listing all of them?
Maybe a way to loop through all the types currently existing in a realm?


回答1:


You can access the types from your Realm configuration, filter them to exclude the one you want to keep than delete each object of each type that you don't want to keep.

let typeToBeKept = MyObjectClass.self
realm.configuration.objectTypes?.filter{$0 != typeToBeKept}.forEach{ type in
    try! realm.write {
        realm.delete(realm.objects(type.self))
    }
}


来源:https://stackoverflow.com/questions/45791715/way-to-purge-all-but-one-object-types-models-in-a-realm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!