Query

Query is a class used to create type-safe query predicates.

With Query you are given the ability to create Swift style query expression that will then be constructed into an NSPredicate. The Query class should not be instantiated directly and should be only used as a parameter within a closure that takes a query expression as an argument. Example:

public func where(_ query: ((Query<Element>) -> Query<Element>)) -> Results<Element>

You would then use the above function like so:

let results = realm.objects(Person.self).query {
   $0.name == "Foo" || $0.name == "Bar" && $0.age >= 21
}

Supported predicate types

Prefix

  • NOT ! swift let results = realm.objects(Person.self).query { !$0.dogsName.contains("Fido") || !$0.name.contains("Foo") }

Comparisions

  • Equals ==
  • Not Equals !=
  • Greater Than >
  • Less Than <
  • Greater Than or Equal >=
  • Less Than or Equal <=
  • Between .contains(_ range:)

Collections

  • IN .contains(_ element:)
  • Between .contains(_ range:)

Map

  • @allKeys .keys
  • @allValues .values

Compound

  • AND &&
  • OR ||

Collection Aggregation

  • @avg .avg
  • @min .min
  • @max .max
  • @sum .sum
  • @count .count swift let results = realm.objects(Person.self).query { !$0.dogs.age.avg >= 0 || !$0.dogsAgesArray.avg >= 0 }

Other

  • NOT !
  • Subquery ($0.fooList.intCol >= 5).count > n

In

  • Checks if the value is present in the collection.

Query Construction

  • For testing purposes only. Do not use directly.

  • Constructs an NSPredicate compatibe string with its accompanying arguments.

    Note

    This is for internal use only and is exposed for testing purposes.

RealmCollection

  • Query the count of the objects in the collection.

  • Checks if an element exists in this collection.

  • Checks if any elements contained in the given array are present in the collection.

  • min

    Returns the minimum value in the collection.

  • max

    Returns the maximum value in the collection.

  • avg

    Returns the average in the collection.

  • sum

    Returns the sum of all the values in the collection.

RealmKeyedCollection

  • Allows a query over all values in the Map.

  • Allows a query over all keys in the Map.

PersistableEnum

  • Query on the rawValue of the Enum rather than the Enum itself.

    This can be used to write queries which can be expressed on the RawValue but not the enum. For example, this lets you query for .starts(with:) on a string enum where the prefix is not a member of the enum.

CustomPersistable

  • Query on the persistableValue of the value rather than the value itself.

    This can be used to write queries which can be expressed on the persisted type but not on the type itself, such as range queries on the persistable value or to query for values which can’t be converted to the mapped type.

    For types which don’t conform to PersistableEnum, CustomPersistable or FailableCustomPersistable this doesn’t do anything useful.

_QueryString

  • Checks for all elements in this collection that equal the given value. ? and * are allowed as wildcard characters, where ? matches 1 character and * matches 0 or more characters.

_QueryBinary