快速过滤领域列表 [英] Filter in realm list swift

查看:36
本文介绍了快速过滤领域列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class RetailerModel: BaseResponse {
var customFields = List<CustomFieldsModel>()
}

class CustomFieldsModel: BaseResponse {
@objc dynamic var params : ParamsModel?
}

class ParamsModel: BaseResponse {
var options = List<String>()
}

我正在使用领域来获取数据并 RetailerModel 的对象为 CustomFieldsModel ,而 CustomFieldsModel 的对象为 ParamsModel ParamsModel 包含我要在其上过滤我的RetailerModel数据的数组

I'm using realm to get data and RetailerModel has an object of CustomFieldsModel and CustomFieldsModel has an object of ParamsModel and ParamsModel contains the array on which i want filter my RetailerModel data

这是 ParamsModel 包含的json数组选项":[自定义",塔"]

this is array from json that ParamsModel contains "options": ["custom", "Towers"]

我想要的只是具有选项自定义"

what I want is just a List of all RetailerModel that have the option "custom"

推荐答案

这是一个非常具有挑战性的查询,但我认为我有解决方案.

This was a very challenging query but I think I have a solution.

但是首先;领域列表中的基元不受很好的支持,并且尚不可查询.请参阅此处阅读更多.

First however; Primitives in Realm Lists are not well supported and are not yet queryable. See here and this for more reading.

编辑:版本10.7添加了对过滤器/查询以及原始函数的聚合功能的支持,因此以下信息不再完全有效.但是,仍然需要注意.

Release 10.7 added support for filters/queries as well as aggregate functions on primitives so the below info is no longer completely valid. However, it's still something to be aware of.

所以我们将需要添加另一个类来保存字符串数据

So we will need to add another class to hold the string data

class StringClass: Object {
    @objc dynamic var myString = ""
}

因此以下课程将进行更新以匹配

so the following class will be updated to match

class ParamsModel: BaseResponse {
    var options = List<StringClass>()
}

然后要执行查询,由于对象的深度,我们需要利用子查询.

Then to peform the query we need to leverage a Subquery due to the depth of the objects.

let predicate = NSPredicate(format: "SUBQUERY(customFields, $customField, ANY $customField.params.options.myString == 'custom').@count > 0")
let results = realm.objects(RetailerModel.self).filter(predicate)

这将返回所有CustomFieldsModel具有ParamsModel选项的所有零售商模型.List属性StringClass myString属性等于'custom'

This will return all Retailer Models whose CustomFieldsModel has a ParamsModel options List property StringClass myString property equal to 'custom'

这篇关于快速过滤领域列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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