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

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

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

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

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"

1

1 Answers

0
votes

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.

EDIT: 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)

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