清除领域中除一种对象类型(模型)以外的所有对象的方法 [英] way to purge all but one object types (models) in a realm

查看:69
本文介绍了清除领域中除一种对象类型(模型)以外的所有对象的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了一个模型外,我想realm.delete()所有模型.有什么方法可以在不列出所有人的情况下进行此操作吗?
也许是一种遍历领域中当前存在的所有类型的方法?

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?

推荐答案

您可以从Realm配置中访问类型,对其进行过滤以排除要保留的类型,然后删除不需要的每种类型的每个对象保持.

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

这篇关于清除领域中除一种对象类型(模型)以外的所有对象的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆