Search code examples
iosswiftrealmnspredicate

Realm - filter objects greater than string attribute


I am trying to filter objects from Realm database. I have attribute itemIdForDateOrder which is string (looks something like this: 11e6-eef3-09306910-baa7-417b3207abf6) and I have no problem with sorting by this attribute:

realm.objects(ItemInfo.self).sorted(byKeyPath: "itemIdForDateOrder", ascending: false)

Problem is that I am trying to filter objects by this attribute and I want to get objects that has this parameter greater.

Part of my predicate:

if let item = newerThan {
   formatString += " AND itemIdForDateOrder > '" + item.itemIdForDateOrder + "'"
}

But when I used it I get exception:

Terminating app due to uncaught exception 'Invalid operator type', reason: 'Operator '>' not supported for string type'

But comparing is working for string types when I can user my attribute with sort. So is there a way how can I filter by this attribute as well? Or I must get all objects and then filtered them (not by realm predicate filter). Thanks


Solution

  • As the error says, Realm's query engine does not currently support filtering string properties using inequalities. I'd suggest filing an enhancement request against Realm requesting that such support be added, and then performing the filtering outside of Realm (i.e., gather the objects into an array and then filter the array).