QueryableSource
public protocol QueryableSource : AnyObject
Encapsulates containers which manages an internal NSManagedObjectContext, such as DataStacks 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
QueryClauses. Requires at least aSelectclause, and optionalWhere,OrderBy,GroupBy, andTweakclauses.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 : QueryableAttributeTypeParameters
froma
Fromclause indicating the entity typeselectClausea
Select<U>clause indicating the properties to fetch, and with the generic type indicating the return type.queryClausesa series of
QueryClauseinstances for the query request. AcceptsWhere,OrderBy,GroupBy, andTweakclauses.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
QueryClauses. Requires at least aSelectclause, and optionalWhere,OrderBy,GroupBy, andTweakclauses.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 : QueryableAttributeTypeParameters
froma
Fromclause indicating the entity typeselectClausea
Select<U>clause indicating the properties to fetch, and with the generic type indicating the return type.queryClausesa series of
QueryClauseinstances for the query request. AcceptsWhere,OrderBy,GroupBy, andTweakclauses.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
QueryChainableBuilderTypebuilt 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 : QueryableAttributeTypeParameters
clauseChaina
QueryChainableBuilderTypeindicating 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
QueryClauses. Requires at least aSelectclause, and optionalWhere,OrderBy,GroupBy, andTweakclauses.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 : DynamicObjectParameters
froma
Fromclause indicating the entity typeselectClausea
Select<U>clause indicating the properties to fetch, and with the generic type indicating the return type.queryClausesa series of
QueryClauseinstances for the query request. AcceptsWhere,OrderBy,GroupBy, andTweakclauses.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
QueryClauses. Requires at least aSelectclause, and optionalWhere,OrderBy,GroupBy, andTweakclauses.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 : DynamicObjectParameters
froma
Fromclause indicating the entity typeselectClausea
Select<U>clause indicating the properties to fetch, and with the generic type indicating the return type.queryClausesa series of
QueryClauseinstances for the query request. AcceptsWhere,OrderBy,GroupBy, andTweakclauses.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
QueryChainableBuilderTypebuilt 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 == NSDictionaryParameters
clauseChaina
QueryChainableBuilderTypeindicating 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
NSManagedObjectContextmanaged 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
View on GitHub
QueryableSource Protocol Reference