QueryableSource
public protocol QueryableSource : AnyObject
Encapsulates containers which manages an internal NSManagedObjectContext
, such as DataStack
s and transactions, that can be used for querying values. CoreStore provides implementations for this protocol and should be used as a read-only abstraction.
-
Queries aggregate values as specified by the
QueryClause
s. Requires at least aSelect
clause, and optionalWhere
,OrderBy
,GroupBy
, andTweak
clauses.A
query
differs from afetch
in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.Declaration
Swift
func queryValue<D, U>(_ from: From<D>, _ selectClause: Select<D, U>, _ queryClauses: QueryClause...) -> U? where D : DynamicObject, U : QueryableAttributeType
Parameters
from
a
From
clause indicating the entity typeselectClause
a
Select<U>
clause indicating the properties to fetch, and with the generic type indicating the return type.queryClauses
a series of
QueryClause
instances for the query request. AcceptsWhere
,OrderBy
,GroupBy
, andTweak
clauses.Return Value
the result of the the query. The type of the return value is specified by the generic type of the
Select<U>
parameter. -
Queries aggregate values as specified by the
QueryClause
s. Requires at least aSelect
clause, and optionalWhere
,OrderBy
,GroupBy
, andTweak
clauses.A
query
differs from afetch
in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.Declaration
Swift
func queryValue<D, U>(_ from: From<D>, _ selectClause: Select<D, U>, _ queryClauses: [QueryClause]) -> U? where D : DynamicObject, U : QueryableAttributeType
Parameters
from
a
From
clause indicating the entity typeselectClause
a
Select<U>
clause indicating the properties to fetch, and with the generic type indicating the return type.queryClauses
a series of
QueryClause
instances for the query request. AcceptsWhere
,OrderBy
,GroupBy
, andTweak
clauses.Return Value
the result of the the query. The type of the return value is specified by the generic type of the
Select<U>
parameter. -
Queries a property value or aggregate as specified by the
QueryChainableBuilderType
built from a chain of clauses.A
query
differs from afetch
in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.let averageAdultAge = dataStack.queryValue( From<MyPersonEntity>() .select(Int.self, .average(\.age)) .where(\.age > 18) )
Declaration
Swift
func queryValue<B>(_ clauseChain: B) -> B.ResultType? where B : QueryChainableBuilderType, B.ResultType : QueryableAttributeType
Parameters
clauseChain
a
QueryChainableBuilderType
indicating the property/aggregate to fetch and the series of queries for the request.Return Value
the result of the the query as specified by the
QueryChainableBuilderType
-
Queries a dictionary of attribute values as specified by the
QueryClause
s. Requires at least aSelect
clause, and optionalWhere
,OrderBy
,GroupBy
, andTweak
clauses.A
query
differs from afetch
in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.Declaration
Swift
func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<D, NSDictionary>, _ queryClauses: QueryClause...) -> [[String : Any]]? where D : DynamicObject
Parameters
from
a
From
clause indicating the entity typeselectClause
a
Select<U>
clause indicating the properties to fetch, and with the generic type indicating the return type.queryClauses
a series of
QueryClause
instances for the query request. AcceptsWhere
,OrderBy
,GroupBy
, andTweak
clauses.Return Value
the result of the the query. The type of the return value is specified by the generic type of the
Select<U>
parameter. -
Queries a dictionary of attribute values as specified by the
QueryClause
s. Requires at least aSelect
clause, and optionalWhere
,OrderBy
,GroupBy
, andTweak
clauses.A
query
differs from afetch
in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.Declaration
Swift
func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<D, NSDictionary>, _ queryClauses: [QueryClause]) -> [[String : Any]]? where D : DynamicObject
Parameters
from
a
From
clause indicating the entity typeselectClause
a
Select<U>
clause indicating the properties to fetch, and with the generic type indicating the return type.queryClauses
a series of
QueryClause
instances for the query request. AcceptsWhere
,OrderBy
,GroupBy
, andTweak
clauses.Return Value
the result of the the query. The type of the return value is specified by the generic type of the
Select<U>
parameter. -
Queries a dictionary of attribute values or as specified by the
QueryChainableBuilderType
built from a chain of clauses.A
query
differs from afetch
in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.let results = source.queryAttributes( From<MyPersonEntity>() .select( NSDictionary.self, .attribute(\.age, as: "age"), .count(\.age, as: "numberOfPeople") ) .groupBy(\.age) ) for dictionary in results! { let age = dictionary["age"] as! Int let count = dictionary["numberOfPeople"] as! Int print("There are \(count) people who are \(age) years old." }
Declaration
Swift
func queryAttributes<B>(_ clauseChain: B) -> [[String : Any]]? where B : QueryChainableBuilderType, B.ResultType == NSDictionary
Parameters
clauseChain
a
QueryChainableBuilderType
indicating the properties to fetch and the series of queries for the request.Return Value
the result of the the query as specified by the
QueryChainableBuilderType
-
The internal
NSManagedObjectContext
managed by thisQueryableSource
. Using this context directly should typically be avoided, and is provided by CoreStore only for extremely specialized cases.Declaration
Swift
func unsafeContext() -> NSManagedObjectContext