Query

public struct Query<E> where E : Entity

A reusable query returning entities.

You can store a Query once it is set up and re-evaluate it e.g. using find() or all().

Use PropertyQuery instead if you want to return aggregate results. Use property(_:) to obtain a PropertyQuery.

PropertyQuery will respect the conditions of its base Query. So it you want to find the average age of all Persons above 30, this is how you can write it:

 let query = personBox.query { Person.age > 29 }
 let ageQuery = query.property(Person.age)
 let averageAge = ageQuery.average
  • The entity type this query is going to target.

    Declaration

    Swift

    public typealias EntityType = E
  • Find all Objects matching the query between the given offset and limit.

    Declaration

    Swift

    public func find(offset: UInt64 = 0, limit: UInt64 = 0) -> [EntityType]

    Parameters

    offset

    How many results to skip. (Useful when paginating results.)

    limit

    Maximum number of objects that may be returned (may give fewer).

    Return Value

    Collection of objects matching the query conditions.

  • Find the first Object matching the query.

    Declaration

    Swift

    public var first: EntityType? { get }
  • Find the single object matching the query.

    When count > 1, this will throw.

    Throws

    NSError with OBXErrorDomain, for example when there is more than one match.

    Declaration

    Swift

    public func findUnique() throws -> EntityType

    Return Value

    The one and only object matching the query conditions.

  • The number of objects matching the query.

    Declaration

    Swift

    public var count: Int { get }
  • all

    Find all Objects matching the query.

    Declaration

    Swift

    public var all: [EntityType] { get }
  • Accessor to get a PropertyQuery object based on the query conditions.

    You use Query to get objects, PropertyQuery to get aggregate results for entity properties. For example, for an Int property, you can get the average, sum, min, and max, among other things.

    Declaration

    Swift

    public func property<T>(_ property: Property<EntityType, T>) -> PropertyQuery<EntityType, T>
        where T: EntityPropertyTypeConvertible

    Parameters

    property

    Object property to modify the query for.

    Return Value

    New PropertyQuery to configure.

  • Sets a parameter of a condition previously specified during query construction to a new value.

    This is the binary operator variant. It changes the value no matter which operation you used, e.g. isEqual/== or isGreaterThan/>.

    See setParameters(property:to:_:) for operators with 2 values.

    If you have multiple conditions on the same property, specify a PropertyAlias so you can choose which condition’s value to change.

    Declaration

    Swift

    public func setParameter<L>(property: Property<EntityType, L>, to value: L) where L : LongPropertyQueryType

    Parameters

    property

    Entity property specification.

    value

    New value for the condition.

  • Sets a parameter previously specified using a ParameterAlias to a new value.

    This is the binary operator variant. See setParameters(alias:to:_:) for operators with 2 values.

    Declaration

    Swift

    public func setParameter<L>(alias: String, to value: L) where L : LongPropertyQueryType

    Parameters

    alias

    Condition’s alias.

    value

    New value.

  • Sets a parameter of a condition previously specified during query construction to new values.

    This is the variant with 2 values, e.g. for isBetween(_:and:) comparison.

    See setParameter(property:to:) for operators with 1 value.

    If you have multiple conditions on the same property, specify a PropertyAlias so you can choose which condition’s value to change.

    Declaration

    Swift

    public func setParameters<L>(property: Property<EntityType, L>, to value1: L, _ value2: L)
        where L: LongPropertyQueryType

    Parameters

    property

    Entity property specification.

    value1

    New first value for the condition.

    value2

    New second value for the condition.

  • Sets a parameter previously specified using a ParameterAlias to new values.

    This is the variant with 2 values, e.g. for isBetween(_:and:) comparison. See setParameter(alias:to:) for operators with 1 value.

    Declaration

    Swift

    public func setParameters<L>(alias: String, to value1: L, _ value2: L) where L : LongPropertyQueryType

    Parameters

    alias

    Condition’s alias.

    value1

    New first value for the condition.

    value2

    New second value for the condition.

  • Sets a parameter previously specified using a ParameterAlias to a new collection value.

    This is used to change the value of e.g. isContained(in:) and similar operations.

    If you have multiple conditions on the same property, specify a PropertyAlias so you can choose which condition’s value to change.

    Declaration

    Swift

    public func setParameters<L>(property: Property<EntityType, L>, to collection: [L]) where L : LongPropertyQueryType

    Parameters

    property

    Entity property specification.

    collection

    New collection of values for the condition.

  • Sets a parameter previously specified during query construction to a new collection value.

    This is used to change the value of e.g. isContained(in:) and similar operations.

    Declaration

    Swift

    public func setParameters<L>(alias: String, to collection: [L]) where L : LongPropertyQueryType

    Parameters

    alias

    Condition’s alias.

    collection

    New collection of values for the condition.

  • Sets a parameter of a condition previously specified during query construction to a new value.

    This is the binary operator variant. It changes the value no matter which operation you used, e.g. isEqual/== or isGreaterThan/>.

    See setParameters(property:to:_:) for operators with 2 values.

    If you have multiple conditions on the same property, specify a PropertyAlias so you can choose which condition’s value to change.

    Declaration

    Swift

    public func setParameter<I>(property: Property<EntityType, I>, to value: I)
        where I: IntegerPropertyQueryType

    Parameters

    property

    Entity property specification.

    value

    New value for the condition.

  • Sets a parameter previously specified using a ParameterAlias to a new value.

    This is the binary operator variant. See setParameters(alias:to:_:) for operators with 2 values.

    Declaration

    Swift

    public func setParameter<I>(alias: String, to value: I) where I : IntegerPropertyQueryType

    Parameters

    alias

    Condition’s alias.

    value

    New value.

  • Sets a parameter of a condition previously specified during query construction to new values.

    This is the variant with 2 values, e.g. for isBetween(_:and:) comparison.

    See setParameter(property:to:) for operators with 1 value.

    If you have multiple conditions on the same property, specify a PropertyAlias so you can choose which condition’s value to change.

    Declaration

    Swift

    public func setParameters<I>(property: Property<EntityType, I>, to value1: I, _ value2: I)
        where I: IntegerPropertyQueryType

    Parameters

    property

    Entity property specification.

    value1

    New first value for the condition.

    value2

    New second value for the condition.

  • Sets a parameter previously specified using a ParameterAlias to new values.

    This is the variant with 2 values, e.g. for isBetween(_:and:) comparison. See setParameter(alias:to:) for operators with 1 value.

    Declaration

    Swift

    public func setParameters<I>(alias: String, to value1: I, _ value2: I)
        where I: IntegerPropertyQueryType

    Parameters

    alias

    Condition’s alias.

    value1

    New first value for the condition.

    value2

    New second value for the condition.

  • Sets a parameter previously specified using a ParameterAlias to a new collection value.

    This is used to change the value of e.g. isContained(in:) and similar operations.

    If you have multiple conditions on the same property, specify a PropertyAlias so you can choose which condition’s value to change.

    Declaration

    Swift

    public func setParameters<I>(property: Property<EntityType, I>, to collection: [I])
        where I: IntegerPropertyQueryType

    Parameters

    property

    Entity property specification.

    collection

    New collection of values for the condition.

  • Sets a parameter previously specified during query construction to a new collection value.

    This is used to change the value of e.g. isContained(in:) and similar operations.

    Declaration

    Swift

    public func setParameters<I>(alias: String, to collection: [I])
        where I: IntegerPropertyQueryType

    Parameters

    alias

    Condition’s alias.

    collection

    New collection of values for the condition.

  • Sets a parameter of a condition previously specified during query construction to a new value.

    This is the binary operator variant. It changes the value no matter which operation you used, e.g. isEqual/== or isGreaterThan/>.

    See setParameters(property:to:_:) for operators with 2 values.

    If you have multiple conditions on the same property, specify a PropertyAlias so you can choose which condition’s value to change.

    Declaration

    Swift

    public func setParameter(property: Property<EntityType, Double>, to value: Double)

    Parameters

    property

    Entity property specification.

    value

    New value for the condition.

  • Sets a parameter previously specified using a ParameterAlias to a new value.

    This is the binary operator variant. See setParameters(alias:to:_:) for operators with 2 values.

    Declaration

    Swift

    public func setParameter(alias: String, to value: Double)

    Parameters

    alias

    Condition’s alias.

    value

    New value.

  • Convenience for setParameters(property:to:_:) that offers the same API as a floating point equality condition with a tolerance.

    If you have multiple conditions on the same property, specify a PropertyAlias so you can choose which condition’s value to change and use `setParameter(alias:toEqual:tolerance:).

    Declaration

    Swift

    public func setParameter(property: Property<EntityType, Double>, toEqual value: Double, tolerance: Double)

    Parameters

    property

    Entity property specification.

    value

    Value to compare, ± tolerance.

    tolerance

    Tolerance around value.

  • Convenience for setParameters(alias:to:_:) that offers the same API as a floating point equality condition with a tolerance.

    Declaration

    Swift

    public func setParameters(alias: String, toEqual value: Double, tolerance: Double)

    Parameters

    alias

    Condition’s alias.

    value

    Value to compare, ± tolerance.

    tolerance

    Tolerance around value.

  • Sets a parameter of a condition previously specified during query construction to new values.

    This is the variant with 2 values, e.g. for isBetween(_:and:) comparison.

    See setParameter(property:to:) for operators with 1 value.

    If you have multiple conditions on the same property, specify a PropertyAlias so you can choose which condition’s value to change.

    Declaration

    Swift

    public func setParameters(property: Property<EntityType, Double>, to value1: Double, _ value2: Double)

    Parameters

    property

    Entity property specification.

    value1

    New first value for the condition.

    value2

    New second value for the condition.

  • Sets a parameter previously specified using a ParameterAlias to new values.

    This is the variant with 2 values, e.g. for isBetween(_:and:) comparison. See setParameter(alias:to:) for operators with 1 value.

    Declaration

    Swift

    public func setParameters(alias: String, to value1: Double, _ value2: Double)

    Parameters

    alias

    Condition’s alias.

    value1

    New first value for the condition.

    value2

    New second value for the condition.

  • Sets a parameter of a condition previously specified during query construction to a new value.

    This is the binary operator variant. It changes the value no matter which operation you used, e.g. isEqual/== or isGreaterThan/>.

    See setParameters(property:to:_:) for operators with 2 values.

    If you have multiple conditions on the same property, specify a PropertyAlias so you can choose which condition’s value to change.

    Declaration

    Swift

    public func setParameter(property: Property<EntityType, String>, to string: String)

    Parameters

    property

    Entity property specification.

    value

    New value for the condition.

  • Sets a parameter previously specified using a ParameterAlias to a new value.

    This is the binary operator variant. See setParameters(alias:to:_:) for operators with 2 values.

    Declaration

    Swift

    public func setParameter(alias: String, to string: String)

    Parameters

    alias

    Condition’s alias.

    value

    New value.

  • Sets a parameter previously specified using a ParameterAlias to a new collection value.

    This is used to change the value of e.g. isContained(in:) and similar operations.

    If you have multiple conditions on the same property, specify a PropertyAlias so you can choose which condition’s value to change.

    Declaration

    Swift

    public func setParameters(property: Property<EntityType, String>, to collection: [String])

    Parameters

    property

    Entity property specification.

    collection

    New collection of values for the condition.

  • Sets a parameter previously specified during query construction to a new collection value.

    This is used to change the value of e.g. isContained(in:) and similar operations.

    Declaration

    Swift

    public func setParameters(alias: String, to collection: [String])

    Parameters

    alias

    Condition’s alias.

    collection

    New collection of values for the condition.