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

后端 未结 1 582
执笔经年
执笔经年 2021-01-16 05:36

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 c

1条回答
  •  礼貌的吻别
    2021-01-16 06:25

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

    0 讨论(0)
提交回复
热议问题