WIP: query chains!

This commit is contained in:
John Rommel Estropia
2017-07-09 10:44:53 +09:00
parent aff966aac9
commit 535eb76adc
40 changed files with 1783 additions and 1004 deletions

View File

@@ -100,7 +100,7 @@ public final class AsynchronousDataTransaction: BaseDataTransaction {
- parameter into: the `Into` clause indicating the destination `NSManagedObject` or `CoreStoreObject` entity type and the destination configuration
- returns: a new `NSManagedObject` or `CoreStoreObject` instance of the specified entity type.
*/
public override func create<T>(_ into: Into<T>) -> T {
public override func create<D>(_ into: Into<D>) -> D {
CoreStore.assert(
!self.isCommitted,
@@ -116,7 +116,7 @@ public final class AsynchronousDataTransaction: BaseDataTransaction {
- parameter object: the `NSManagedObject` or `CoreStoreObject` to be edited
- returns: an editable proxy for the specified `NSManagedObject` or `CoreStoreObject`.
*/
public override func edit<T: DynamicObject>(_ object: T?) -> T? {
public override func edit<D: DynamicObject>(_ object: D?) -> D? {
CoreStore.assert(
!self.isCommitted,
@@ -133,7 +133,7 @@ public final class AsynchronousDataTransaction: BaseDataTransaction {
- parameter objectID: the `NSManagedObjectID` for the object to be edited
- returns: an editable proxy for the specified `NSManagedObject` or `CoreStoreObject`.
*/
public override func edit<T>(_ into: Into<T>, _ objectID: NSManagedObjectID) -> T? {
public override func edit<D>(_ into: Into<D>, _ objectID: NSManagedObjectID) -> D? {
CoreStore.assert(
!self.isCommitted,
@@ -148,7 +148,7 @@ public final class AsynchronousDataTransaction: BaseDataTransaction {
- parameter object: the `NSManagedObject` or `CoreStoreObject` to be deleted
*/
public override func delete<T: DynamicObject>(_ object: T?) {
public override func delete<D: DynamicObject>(_ object: D?) {
CoreStore.assert(
!self.isCommitted,
@@ -165,7 +165,7 @@ public final class AsynchronousDataTransaction: BaseDataTransaction {
- parameter object2: another `DynamicObject` to be deleted
- parameter objects: other `DynamicObject`s to be deleted
*/
public override func delete<T: DynamicObject>(_ object1: T?, _ object2: T?, _ objects: T?...) {
public override func delete<D: DynamicObject>(_ object1: D?, _ object2: D?, _ objects: D?...) {
CoreStore.assert(
!self.isCommitted,

View File

@@ -39,9 +39,9 @@ public extension BaseDataTransaction {
- throws: an `Error` thrown from any of the `ImportableObject` methods
- returns: the created `ImportableObject` instance, or `nil` if the import was ignored
*/
public func importObject<T: DynamicObject & ImportableObject>(
_ into: Into<T>,
source: T.ImportSource) throws -> T? {
public func importObject<D: ImportableObject>(
_ into: Into<D>,
source: D.ImportSource) throws -> D? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -69,9 +69,9 @@ public extension BaseDataTransaction {
- parameter source: the object to import values from
- throws: an `Error` thrown from any of the `ImportableObject` methods
*/
public func importObject<T: DynamicObject & ImportableObject>(
_ object: T,
source: T.ImportSource) throws {
public func importObject<D: ImportableObject>(
_ object: D,
source: D.ImportSource) throws {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -97,9 +97,9 @@ public extension BaseDataTransaction {
- throws: an `Error` thrown from any of the `ImportableObject` methods
- returns: the array of created `ImportableObject` instances
*/
public func importObjects<T: DynamicObject & ImportableObject, S: Sequence>(
_ into: Into<T>,
sourceArray: S) throws -> [T] where S.Iterator.Element == T.ImportSource {
public func importObjects<D: ImportableObject, S: Sequence>(
_ into: Into<D>,
sourceArray: S) throws -> [D] where S.Iterator.Element == D.ImportSource {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -108,7 +108,7 @@ public extension BaseDataTransaction {
return try autoreleasepool {
return try sourceArray.flatMap { (source) -> T? in
return try sourceArray.flatMap { (source) -> D? in
let entityType = into.entityClass
guard entityType.shouldInsert(from: source, in: self) else {
@@ -133,9 +133,9 @@ public extension BaseDataTransaction {
- throws: an `Error` thrown from any of the `ImportableUniqueObject` methods
- returns: the created/updated `ImportableUniqueObject` instance, or `nil` if the import was ignored
*/
public func importUniqueObject<T: DynamicObject & ImportableUniqueObject>(
_ into: Into<T>,
source: T.ImportSource) throws -> T? {
public func importUniqueObject<D: ImportableUniqueObject>(
_ into: Into<D>,
source: D.ImportSource) throws -> D? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -151,7 +151,7 @@ public extension BaseDataTransaction {
return nil
}
if let object = self.fetchOne(From(entityType), Where(uniqueIDKeyPath, isEqualTo: uniqueIDValue)) {
if let object = self.fetchOne(From(entityType), Where<D>(uniqueIDKeyPath, isEqualTo: uniqueIDValue)) {
guard entityType.shouldUpdate(from: source, in: self) else {
@@ -185,10 +185,10 @@ public extension BaseDataTransaction {
- throws: an `Error` thrown from any of the `ImportableUniqueObject` methods
- returns: the array of created/updated `ImportableUniqueObject` instances
*/
public func importUniqueObjects<T: DynamicObject & ImportableUniqueObject, S: Sequence>(
_ into: Into<T>,
public func importUniqueObjects<D: ImportableUniqueObject, S: Sequence>(
_ into: Into<D>,
sourceArray: S,
preProcess: @escaping (_ mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource] = { $0 }) throws -> [T] where S.Iterator.Element == T.ImportSource {
preProcess: @escaping (_ mapping: [D.UniqueIDType: D.ImportSource]) throws -> [D.UniqueIDType: D.ImportSource] = { $0 }) throws -> [D] where S.Iterator.Element == D.ImportSource {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -198,10 +198,10 @@ public extension BaseDataTransaction {
return try autoreleasepool {
let entityType = into.entityClass
var importSourceByID = Dictionary<T.UniqueIDType, T.ImportSource>()
var importSourceByID = Dictionary<D.UniqueIDType, D.ImportSource>()
let sortedIDs = try autoreleasepool {
return try sourceArray.flatMap { (source) -> T.UniqueIDType? in
return try sourceArray.flatMap { (source) -> D.UniqueIDType? in
guard let uniqueIDValue = try entityType.uniqueID(from: source, in: self) else {
@@ -214,12 +214,12 @@ public extension BaseDataTransaction {
importSourceByID = try autoreleasepool { try preProcess(importSourceByID) }
var existingObjectsByID = Dictionary<T.UniqueIDType, T>()
self.fetchAll(From(entityType), Where(entityType.uniqueIDKeyPath, isMemberOf: sortedIDs))?
var existingObjectsByID = Dictionary<D.UniqueIDType, D>()
self.fetchAll(From(entityType), Where<D>(entityType.uniqueIDKeyPath, isMemberOf: sortedIDs))?
.forEach { existingObjectsByID[$0.uniqueIDValue] = $0 }
var processedObjectIDs = Set<T.UniqueIDType>()
var result = [T]()
var processedObjectIDs = Set<D.UniqueIDType>()
var result = [D]()
for objectID in sortedIDs where !processedObjectIDs.contains(objectID) {

View File

@@ -39,13 +39,12 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- returns: the number of `DynamicObject`s deleted
*/
@discardableResult
public func deleteAll<T>(_ from: From<T>, _ deleteClauses: DeleteClause...) -> Int? {
public func deleteAll<D>(_ from: From<D>, _ deleteClauses: DeleteClause...) -> Int? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
"Attempted to delete from a \(cs_typeName(self)) outside its designated queue."
)
return self.context.deleteAll(from, deleteClauses)
}
@@ -57,14 +56,32 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- returns: the number of `DynamicObject`s deleted
*/
@discardableResult
public func deleteAll<T>(_ from: From<T>, _ deleteClauses: [DeleteClause]) -> Int? {
public func deleteAll<D>(_ from: From<D>, _ deleteClauses: [DeleteClause]) -> Int? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
"Attempted to delete from a \(cs_typeName(self)) outside its designated queue."
)
return self.context.deleteAll(from, deleteClauses)
}
/**
Deletes all `DynamicObject`s that satisfy the specified conditions.
```
transaction.deleteAll(From<Person>().where(\.age > 50)
```
- parameter clauseChain: a `FetchChainableBuilderType` clause chain created from a `From` clause
- returns: the number of `DynamicObject`s deleted
*/
@discardableResult
public func deleteAll<B: FetchChainableBuilderType>(_ clauseChain: B) -> Int? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
"Attempted to delete from a \(cs_typeName(self)) outside its designated queue."
)
return self.context.deleteAll(from, deleteClauses)
return self.context.deleteAll(clauseChain.from, clauseChain.fetchClauses)
}
@@ -76,7 +93,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter object: a reference to the object created/fetched outside the transaction
- returns: the `DynamicObject` instance if the object exists in the transaction, or `nil` if not found.
*/
public func fetchExisting<T: DynamicObject>(_ object: T) -> T? {
public func fetchExisting<D: DynamicObject>(_ object: D) -> D? {
return self.context.fetchExisting(object)
}
@@ -87,7 +104,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter objectID: the `NSManagedObjectID` for the object
- returns: the `DynamicObject` instance if the object exists in the transaction, or `nil` if not found.
*/
public func fetchExisting<T: DynamicObject>(_ objectID: NSManagedObjectID) -> T? {
public func fetchExisting<D: DynamicObject>(_ objectID: NSManagedObjectID) -> D? {
return self.context.fetchExisting(objectID)
}
@@ -98,7 +115,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter objects: an array of `DynamicObject`s created/fetched outside the transaction
- returns: the `DynamicObject` array for objects that exists in the transaction
*/
public func fetchExisting<T: DynamicObject, S: Sequence>(_ objects: S) -> [T] where S.Iterator.Element == T {
public func fetchExisting<D: DynamicObject, S: Sequence>(_ objects: S) -> [D] where S.Iterator.Element == D {
return self.context.fetchExisting(objects)
}
@@ -109,7 +126,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter objectIDs: the `NSManagedObjectID` array for the objects
- returns: the `DynamicObject` array for objects that exists in the transaction
*/
public func fetchExisting<T: DynamicObject, S: Sequence>(_ objectIDs: S) -> [T] where S.Iterator.Element == NSManagedObjectID {
public func fetchExisting<D: DynamicObject, S: Sequence>(_ objectIDs: S) -> [D] where S.Iterator.Element == NSManagedObjectID {
return self.context.fetchExisting(objectIDs)
}
@@ -121,7 +138,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the first `DynamicObject` instance that satisfies the specified `FetchClause`s
*/
public func fetchOne<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> T? {
public func fetchOne<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> D? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -137,7 +154,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the first `DynamicObject` instance that satisfies the specified `FetchClause`s
*/
public func fetchOne<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
public func fetchOne<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> D? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -146,6 +163,16 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
return self.context.fetchOne(from, fetchClauses)
}
// TODO: docs
public func fetchOne<B: FetchChainableBuilderType>(_ clauseChain: B) -> B.ObjectType? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
"Attempted to fetch from a \(cs_typeName(self)) outside its designated queue."
)
return self.context.fetchOne(clauseChain)
}
/**
Fetches all `DynamicObject` instances that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -153,7 +180,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: all `DynamicObject` instances that satisfy the specified `FetchClause`s
*/
public func fetchAll<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [T]? {
public func fetchAll<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> [D]? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -169,7 +196,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: all `DynamicObject` instances that satisfy the specified `FetchClause`s
*/
public func fetchAll<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [T]? {
public func fetchAll<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> [D]? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -178,6 +205,16 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
return self.context.fetchAll(from, fetchClauses)
}
// TODO: docs
public func fetchAll<B: FetchChainableBuilderType>(_ clauseChain: B) -> [B.ObjectType]? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
"Attempted to fetch from a \(cs_typeName(self)) outside its designated queue."
)
return self.context.fetchAll(clauseChain)
}
/**
Fetches the number of `DynamicObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -185,7 +222,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the number `DynamicObject`s that satisfy the specified `FetchClause`s
*/
public func fetchCount<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> Int? {
public func fetchCount<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> Int? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -201,7 +238,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the number `DynamicObject`s that satisfy the specified `FetchClause`s
*/
public func fetchCount<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> Int? {
public func fetchCount<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> Int? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -210,6 +247,16 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
return self.context.fetchCount(from, fetchClauses)
}
// TODO: docs
public func fetchCount<B: FetchChainableBuilderType>(_ clauseChain: B) -> Int? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
"Attempted to fetch from a \(cs_typeName(self)) outside its designated queue."
)
return self.context.fetchCount(clauseChain)
}
/**
Fetches the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -217,7 +264,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s
*/
public func fetchObjectID<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
public func fetchObjectID<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -233,7 +280,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s
*/
public func fetchObjectID<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
public func fetchObjectID<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -242,6 +289,16 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
return self.context.fetchObjectID(from, fetchClauses)
}
// TODO: docs
public func fetchObjectID<B: FetchChainableBuilderType>(_ clauseChain: B) -> NSManagedObjectID? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
"Attempted to fetch from a \(cs_typeName(self)) outside its designated queue."
)
return self.context.fetchObjectID(clauseChain)
}
/**
Fetches the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -249,7 +306,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s
*/
public func fetchObjectIDs<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
public func fetchObjectIDs<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -265,7 +322,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s
*/
public func fetchObjectIDs<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
public func fetchObjectIDs<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -274,6 +331,16 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
return self.context.fetchObjectIDs(from, fetchClauses)
}
// TODO: docs
public func fetchObjectIDs<B: FetchChainableBuilderType>(_ clauseChain: B) -> [NSManagedObjectID]? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
"Attempted to fetch from a \(cs_typeName(self)) outside its designated queue."
)
return self.context.fetchObjectIDs(clauseChain)
}
// MARK: QueryableSource
@@ -287,7 +354,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
public func queryValue<T, U: QueryableAttributeType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
public func queryValue<D, U: QueryableAttributeType>(_ from: From<D>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -306,7 +373,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
public func queryValue<T, U: QueryableAttributeType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
public func queryValue<D, U: QueryableAttributeType>(_ from: From<D>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -325,7 +392,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
public func queryAttributes<T>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]? {
public func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -344,7 +411,7 @@ extension BaseDataTransaction: FetchableSource, QueryableSource {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
public func queryAttributes<T>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]? {
public func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]? {
CoreStore.assert(
self.isRunningInAllowedQueue(),

View File

@@ -50,7 +50,7 @@ public /*abstract*/ class BaseDataTransaction {
- parameter into: the `Into` clause indicating the destination `NSManagedObject` or `CoreStoreObject` entity type and the destination configuration
- returns: a new `NSManagedObject` or `CoreStoreObject` instance of the specified entity type.
*/
public func create<T>(_ into: Into<T>) -> T {
public func create<D>(_ into: Into<D>) -> D {
let entityClass = into.entityClass
CoreStore.assert(
@@ -121,7 +121,7 @@ public /*abstract*/ class BaseDataTransaction {
- parameter object: the `NSManagedObject` type to be edited
- returns: an editable proxy for the specified `NSManagedObject`.
*/
public func edit<T: DynamicObject>(_ object: T?) -> T? {
public func edit<D: DynamicObject>(_ object: D?) -> D? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -141,7 +141,7 @@ public /*abstract*/ class BaseDataTransaction {
- parameter objectID: the `NSManagedObjectID` for the object to be edited
- returns: an editable proxy for the specified `NSManagedObject`.
*/
public func edit<T>(_ into: Into<T>, _ objectID: NSManagedObjectID) -> T? {
public func edit<D>(_ into: Into<D>, _ objectID: NSManagedObjectID) -> D? {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -160,7 +160,7 @@ public /*abstract*/ class BaseDataTransaction {
- parameter object: the `NSManagedObject` to be deleted
*/
public func delete<T: DynamicObject>(_ object: T?) {
public func delete<D: DynamicObject>(_ object: D?) {
CoreStore.assert(
self.isRunningInAllowedQueue(),
@@ -179,7 +179,7 @@ public /*abstract*/ class BaseDataTransaction {
- parameter object2: another `NSManagedObject` to be deleted
- parameter objects: other `NSManagedObject`s to be deleted
*/
public func delete<T: DynamicObject>(_ object1: T?, _ object2: T?, _ objects: T?...) {
public func delete<D: DynamicObject>(_ object1: D?, _ object2: D?, _ objects: D?...) {
self.delete(([object1, object2] + objects).flatMap { $0 })
}
@@ -220,7 +220,7 @@ public /*abstract*/ class BaseDataTransaction {
- parameter entity: the `DynamicObject` subclass to filter
- returns: a `Set` of pending `DynamicObject`s of the specified type that were inserted to the transaction.
*/
public func insertedObjects<T: DynamicObject>(_ entity: T.Type) -> Set<T> {
public func insertedObjects<D: DynamicObject>(_ entity: D.Type) -> Set<D> {
CoreStore.assert(
self.transactionQueue.cs_isCurrentExecutionContext(),
@@ -257,7 +257,7 @@ public /*abstract*/ class BaseDataTransaction {
- parameter entity: the `DynamicObject` subclass to filter
- returns: a `Set` of pending `NSManagedObjectID`s of the specified type that were inserted to the transaction.
*/
public func insertedObjectIDs<T: DynamicObject>(_ entity: T.Type) -> Set<NSManagedObjectID> {
public func insertedObjectIDs<D: DynamicObject>(_ entity: D.Type) -> Set<NSManagedObjectID> {
CoreStore.assert(
self.transactionQueue.cs_isCurrentExecutionContext(),
@@ -276,7 +276,7 @@ public /*abstract*/ class BaseDataTransaction {
- parameter entity: the `DynamicObject` subclass to filter
- returns: a `Set` of pending `DynamicObject`s of the specified type that were updated in the transaction.
*/
public func updatedObjects<T: DynamicObject>(_ entity: T.Type) -> Set<T> {
public func updatedObjects<D: DynamicObject>(_ entity: D.Type) -> Set<D> {
CoreStore.assert(
self.transactionQueue.cs_isCurrentExecutionContext(),
@@ -313,7 +313,7 @@ public /*abstract*/ class BaseDataTransaction {
- parameter entity: the `DynamicObject` subclass to filter
- returns: a `Set` of pending `NSManagedObjectID`s of the specified type that were updated in the transaction.
*/
public func updatedObjectIDs<T: DynamicObject>(_ entity: T.Type) -> Set<NSManagedObjectID> {
public func updatedObjectIDs<D: DynamicObject>(_ entity: D.Type) -> Set<NSManagedObjectID> {
CoreStore.assert(
self.transactionQueue.cs_isCurrentExecutionContext(),
@@ -332,7 +332,7 @@ public /*abstract*/ class BaseDataTransaction {
- parameter entity: the `DynamicObject` subclass to filter
- returns: a `Set` of pending `DynamicObject`s of the specified type that were deleted from the transaction.
*/
public func deletedObjects<T: DynamicObject>(_ entity: T.Type) -> Set<T> {
public func deletedObjects<D: DynamicObject>(_ entity: D.Type) -> Set<D> {
CoreStore.assert(
self.transactionQueue.cs_isCurrentExecutionContext(),
@@ -370,7 +370,7 @@ public /*abstract*/ class BaseDataTransaction {
- parameter entity: the `DynamicObject` subclass to filter
- returns: a `Set` of pending `NSManagedObjectID`s of the specified type that were deleted from the transaction.
*/
public func deletedObjectIDs<T: DynamicObject>(_ entity: T.Type) -> Set<NSManagedObjectID> {
public func deletedObjectIDs<D: DynamicObject>(_ entity: D.Type) -> Set<NSManagedObjectID> {
CoreStore.assert(
self.transactionQueue.cs_isCurrentExecutionContext(),

View File

@@ -145,7 +145,7 @@ public final class CSFrom: NSObject {
public let bridgeToSwift: From<NSManagedObject>
public init<T: NSManagedObject>(_ swiftValue: From<T>) {
public init<D: NSManagedObject>(_ swiftValue: From<D>) {
self.bridgeToSwift = swiftValue.downcast()
super.init()
@@ -155,7 +155,7 @@ public final class CSFrom: NSObject {
// MARK: - From
extension From where T: NSManagedObject {
extension From where D: NSManagedObject {
// MARK: CoreStoreSwiftType

View File

@@ -112,7 +112,7 @@ public final class CSInto: NSObject {
public let bridgeToSwift: Into<NSManagedObject>
public required init<T: NSManagedObject>(_ swiftValue: Into<T>) {
public required init<D: NSManagedObject>(_ swiftValue: Into<D>) {
self.bridgeToSwift = swiftValue.downcast()
super.init()
@@ -122,7 +122,7 @@ public final class CSInto: NSObject {
// MARK: - Into
extension Into where T: NSManagedObject {
extension Into where D: NSManagedObject {
// MARK: CoreStoreSwiftType

View File

@@ -35,7 +35,7 @@ import CoreData
- SeeAlso: `OrderBy`
*/
@objc
public final class CSOrderBy: NSObject, CSFetchClause, CSQueryClause, CSDeleteClause, CoreStoreObjectiveCType {
public final class CSOrderBy: NSObject, CSFetchClause, CSQueryClause, CSDeleteClause {
/**
The list of sort descriptors
@@ -110,11 +110,11 @@ public final class CSOrderBy: NSObject, CSFetchClause, CSQueryClause, CSDeleteCl
// MARK: CoreStoreObjectiveCType
public let bridgeToSwift: OrderBy
public let bridgeToSwift: OrderBy<NSManagedObject>
public init(_ swiftValue: OrderBy) {
public init<D: NSManagedObject>(_ swiftValue: OrderBy<D>) {
self.bridgeToSwift = swiftValue
self.bridgeToSwift = swiftValue.downcast()
super.init()
}
}
@@ -122,7 +122,7 @@ public final class CSOrderBy: NSObject, CSFetchClause, CSQueryClause, CSDeleteCl
// MARK: - OrderBy
extension OrderBy: CoreStoreSwiftType {
extension OrderBy where D: NSManagedObject {
// MARK: CoreStoreSwiftType
@@ -130,4 +130,12 @@ extension OrderBy: CoreStoreSwiftType {
return CSOrderBy(self)
}
// MARK: FilePrivate
fileprivate func downcast() -> OrderBy<NSManagedObject> {
return OrderBy<NSManagedObject>(self.sortDescriptors)
}
}

View File

@@ -35,7 +35,7 @@ import CoreData
- SeeAlso: `Where`
*/
@objc
public final class CSWhere: NSObject, CSFetchClause, CSQueryClause, CSDeleteClause, CoreStoreObjectiveCType {
public final class CSWhere: NSObject, CSFetchClause, CSQueryClause, CSDeleteClause {
/**
The internal `NSPredicate` instance for the `Where` clause
@@ -149,11 +149,11 @@ public final class CSWhere: NSObject, CSFetchClause, CSQueryClause, CSDeleteClau
// MARK: CoreStoreObjectiveCType
public let bridgeToSwift: Where
public let bridgeToSwift: Where<NSManagedObject>
public init(_ swiftValue: Where) {
public init<D: NSManagedObject>(_ swiftValue: Where<D>) {
self.bridgeToSwift = swiftValue
self.bridgeToSwift = swiftValue.downcast()
super.init()
}
}
@@ -161,7 +161,7 @@ public final class CSWhere: NSObject, CSFetchClause, CSQueryClause, CSDeleteClau
// MARK: - Where
extension Where: CoreStoreSwiftType {
extension Where where D: NSManagedObject {
// MARK: CoreStoreSwiftType
@@ -169,4 +169,12 @@ extension Where: CoreStoreSwiftType {
return CSWhere(self)
}
// MARK: FilePrivate
fileprivate func downcast() -> Where<NSManagedObject> {
return Where<NSManagedObject>(self.predicate)
}
}

View File

@@ -0,0 +1,320 @@
//
// FetchCondition.swift
// CoreStore
//
// Copyright © 2017 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
import CoreData
public protocol FetchChainableBuilderType {
associatedtype ObjectType: DynamicObject
var from: From<ObjectType> { get set }
var fetchClauses: [FetchClause] { get set }
}
public protocol QueryChainableBuilderType {
associatedtype ObjectType: DynamicObject
associatedtype ResultType: SelectResultType
var from: From<ObjectType> { get set }
var select: Select<ResultType> { get set }
var groupBy: GroupBy { get set }
var fetchClauses: [FetchClause] { get set }
}
public protocol SectionMonitorBuilderType {
associatedtype ObjectType: DynamicObject
var from: From<ObjectType> { get set }
var sectionBy: SectionBy { get set }
var fetchClauses: [FetchClause] { get set }
}
// MARK: - FetchChainBuilder
public struct FetchChainBuilder<D: DynamicObject>: FetchChainableBuilderType {
// MARK: FetchChainableBuilderType
public typealias ObjectType = D
public var from: From<D>
public var fetchClauses: [FetchClause] = []
}
// MARK: - QueryChainBuilder
public struct QueryChainBuilder<D: DynamicObject, R: SelectResultType>: QueryChainableBuilderType {
// MARK: QueryChainableBuilderType
public typealias ObjectType = D
public typealias ResultType = R
public var from: From<D>
public var select: Select<R>
public var groupBy: GroupBy
public var fetchClauses: [FetchClause] = []
}
// MARK: - SectionMonitorChainBuilder
public struct SectionMonitorChainBuilder<D: DynamicObject>: SectionMonitorBuilderType {
// MARK: SectionMonitorBuilderType
public var from: From<D>
public var sectionBy: SectionBy
public var fetchClauses: [FetchClause] = []
}
// MARK: - From
public extension From {
public func select<R>(_ resultType: R.Type, _ selectTerm: SelectTerm, _ selectTerms: SelectTerm...) -> QueryChainBuilder<D, R> {
return self.select(resultType, [selectTerm] + selectTerms)
}
public func select<R>(_ resultType: R.Type, _ selectTerms: [SelectTerm]) -> QueryChainBuilder<D, R> {
return .init(
from: self,
select: .init(selectTerms),
groupBy: .init(),
fetchClauses: []
)
}
public func sectionBy(_ sectionKeyPath: KeyPathString) -> SectionMonitorChainBuilder<D> {
return self.sectionBy(sectionKeyPath, { $0 })
}
public func sectionBy(_ sectionKeyPath: KeyPathString, _ sectionIndexTransformer: @escaping (_ sectionName: String?) -> String?) -> SectionMonitorChainBuilder<D> {
return .init(
from: self,
sectionBy: .init(sectionKeyPath, sectionIndexTransformer),
fetchClauses: []
)
}
public func `where`(_ clause: Where<D>) -> FetchChainBuilder<D> {
return self.fetchChain(appending: clause)
}
public func `where`(format: String, _ args: Any...) -> FetchChainBuilder<D> {
return self.fetchChain(appending: Where<D>(format, argumentArray: args))
}
public func `where`(format: String, argumentArray: [Any]?) -> FetchChainBuilder<D> {
return self.fetchChain(appending: Where<D>(format, argumentArray: argumentArray))
}
public func orderBy(_ sortKey: OrderBy<D>.SortKey, _ sortKeys: OrderBy<D>.SortKey...) -> FetchChainBuilder<D> {
return self.fetchChain(appending: OrderBy<D>([sortKey] + sortKeys))
}
public func tweak(_ fetchRequest: @escaping (NSFetchRequest<NSFetchRequestResult>) -> Void) -> FetchChainBuilder<D> {
return self.fetchChain(appending: Tweak(fetchRequest))
}
public func appending(_ clause: FetchClause) -> FetchChainBuilder<D> {
return self.fetchChain(appending: clause)
}
// MARK: Private
private func fetchChain(appending clause: FetchClause) -> FetchChainBuilder<D> {
return .init(from: self, fetchClauses: [clause])
}
}
public extension FetchChainBuilder {
public func `where`(_ clause: Where<D>) -> FetchChainBuilder<D> {
return self.fetchChain(appending: clause)
}
public func `where`(format: String, _ args: Any...) -> FetchChainBuilder<D> {
return self.fetchChain(appending: Where<D>(format, argumentArray: args))
}
public func `where`(format: String, argumentArray: [Any]?) -> FetchChainBuilder<D> {
return self.fetchChain(appending: Where<D>(format, argumentArray: argumentArray))
}
public func orderBy(_ sortKey: OrderBy<D>.SortKey, _ sortKeys: OrderBy<D>.SortKey...) -> FetchChainBuilder<D> {
return self.fetchChain(appending: OrderBy<D>([sortKey] + sortKeys))
}
public func tweak(_ fetchRequest: @escaping (NSFetchRequest<NSFetchRequestResult>) -> Void) -> FetchChainBuilder<D> {
return self.fetchChain(appending: Tweak(fetchRequest))
}
public func appending(_ clause: FetchClause) -> FetchChainBuilder<D> {
return self.fetchChain(appending: clause)
}
// MARK: Private
private func fetchChain(appending clause: FetchClause) -> FetchChainBuilder<D> {
return .init(
from: self.from,
fetchClauses: self.fetchClauses + [clause]
)
}
}
public extension QueryChainBuilder {
public func groupBy(_ keyPath: KeyPathString, _ keyPaths: KeyPathString...) -> QueryChainBuilder<D, R> {
return self.groupBy([keyPath] + keyPaths)
}
public func groupBy(_ keyPaths: [KeyPathString]) -> QueryChainBuilder<D, R> {
return .init(
from: self.from,
select: self.select,
groupBy: .init(keyPaths),
fetchClauses: self.fetchClauses
)
}
public func `where`(_ clause: Where<D>) -> QueryChainBuilder<D, R> {
return self.queryChain(appending: clause)
}
public func `where`(format: String, _ args: Any...) -> QueryChainBuilder<D, R> {
return self.queryChain(appending: Where<D>(format, argumentArray: args))
}
public func `where`(format: String, argumentArray: [Any]?) -> QueryChainBuilder<D, R> {
return self.queryChain(appending: Where<D>(format, argumentArray: argumentArray))
}
public func orderBy(_ sortKey: OrderBy<D>.SortKey, _ sortKeys: OrderBy<D>.SortKey...) -> QueryChainBuilder<D, R> {
return self.queryChain(appending: OrderBy<D>([sortKey] + sortKeys))
}
public func tweak(_ fetchRequest: @escaping (NSFetchRequest<NSFetchRequestResult>) -> Void) -> QueryChainBuilder<D, R> {
return self.queryChain(appending: Tweak(fetchRequest))
}
public func appending(_ clause: FetchClause) -> QueryChainBuilder<D, R> {
return self.queryChain(appending: clause)
}
// MARK: Private
private func queryChain(appending clause: FetchClause) -> QueryChainBuilder<D, R> {
return .init(
from: self.from,
select: self.select,
groupBy: self.groupBy,
fetchClauses: self.fetchClauses + [clause]
)
}
}
public extension SectionMonitorChainBuilder {
public func `where`(_ clause: Where<D>) -> SectionMonitorChainBuilder<D> {
return self.sectionMonitorChain(appending: clause)
}
public func `where`(format: String, _ args: Any...) -> SectionMonitorChainBuilder<D> {
return self.sectionMonitorChain(appending: Where<D>(format, argumentArray: args))
}
public func `where`(format: String, argumentArray: [Any]?) -> SectionMonitorChainBuilder<D> {
return self.sectionMonitorChain(appending: Where<D>(format, argumentArray: argumentArray))
}
public func orderBy(_ sortKey: OrderBy<D>.SortKey, _ sortKeys: OrderBy<D>.SortKey...) -> SectionMonitorChainBuilder<D> {
return self.sectionMonitorChain(appending: OrderBy<D>([sortKey] + sortKeys))
}
public func tweak(_ fetchRequest: @escaping (NSFetchRequest<NSFetchRequestResult>) -> Void) -> SectionMonitorChainBuilder<D> {
return self.sectionMonitorChain(appending: Tweak(fetchRequest))
}
public func appending(_ clause: FetchClause) -> SectionMonitorChainBuilder<D> {
return self.sectionMonitorChain(appending: clause)
}
// MARK: Private
private func sectionMonitorChain(appending clause: FetchClause) -> SectionMonitorChainBuilder<D> {
return .init(
from: self.from,
sectionBy: self.sectionBy,
fetchClauses: self.fetchClauses + [clause]
)
}
}

View File

@@ -43,7 +43,7 @@ public protocol FetchClause {
/**
The `QueryClause` implement clauses used to configure `NSFetchRequest`s.
*/
public protocol QueryClause {
public protocol QueryClause: FetchClause {
func applyToFetchRequest<T>(_ fetchRequest: NSFetchRequest<T>)
}
@@ -54,7 +54,7 @@ public protocol QueryClause {
/**
The `DeleteClause` implement clauses used to configure `NSFetchRequest`s.
*/
public protocol DeleteClause {
public protocol DeleteClause: FetchClause {
func applyToFetchRequest<T>(_ fetchRequest: NSFetchRequest<T>)
}

View File

@@ -38,7 +38,7 @@ public extension CoreStore {
- parameter object: the `DynamicObject` to observe changes from
- returns: a `ObjectMonitor` that monitors changes to `object`
*/
public static func monitorObject<T>(_ object: T) -> ObjectMonitor<T> {
public static func monitorObject<D>(_ object: D) -> ObjectMonitor<D> {
return self.defaultStack.monitorObject(object)
}
@@ -50,7 +50,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list
*/
public static func monitorList<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
public static func monitorList<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> ListMonitor<D> {
return self.defaultStack.monitorList(from, fetchClauses)
}
@@ -62,7 +62,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list
*/
public static func monitorList<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
public static func monitorList<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> ListMonitor<D> {
return self.defaultStack.monitorList(from, fetchClauses)
}
@@ -74,7 +74,7 @@ public extension CoreStore {
- parameter from: a `From` clause indicating the entity type
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/
public static func monitorList<T>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) {
public static func monitorList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ fetchClauses: FetchClause...) {
self.defaultStack.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses)
}
@@ -86,7 +86,7 @@ public extension CoreStore {
- parameter from: a `From` clause indicating the entity type
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/
public static func monitorList<T>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) {
public static func monitorList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ fetchClauses: [FetchClause]) {
self.defaultStack.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses)
}
@@ -99,7 +99,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list
*/
public static func monitorSectionedList<T>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
public static func monitorSectionedList<D>(_ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<D> {
return self.defaultStack.monitorSectionedList(from, sectionBy, fetchClauses)
}
@@ -112,7 +112,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list
*/
public static func monitorSectionedList<T>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
public static func monitorSectionedList<D>(_ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<D> {
return self.defaultStack.monitorSectionedList(from, sectionBy, fetchClauses)
}
@@ -125,7 +125,7 @@ public extension CoreStore {
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/
public static func monitorSectionedList<T>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
public static func monitorSectionedList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
self.defaultStack.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses)
}
@@ -138,7 +138,7 @@ public extension CoreStore {
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/
public static func monitorSectionedList<T>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
public static func monitorSectionedList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
self.defaultStack.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses)
}

View File

@@ -37,7 +37,7 @@ public extension CoreStore {
- parameter object: a reference to the object created/fetched outside the `DataStack`
- returns: the `DynamicObject` instance if the object exists in the `DataStack`, or `nil` if not found.
*/
public static func fetchExisting<T: DynamicObject>(_ object: T) -> T? {
public static func fetchExisting<D: DynamicObject>(_ object: D) -> D? {
return self.defaultStack.fetchExisting(object)
}
@@ -48,7 +48,7 @@ public extension CoreStore {
- parameter objectID: the `NSManagedObjectID` for the object
- returns: the `DynamicObject` instance if the object exists in the `DataStack`, or `nil` if not found.
*/
public static func fetchExisting<T: DynamicObject>(_ objectID: NSManagedObjectID) -> T? {
public static func fetchExisting<D: DynamicObject>(_ objectID: NSManagedObjectID) -> D? {
return self.defaultStack.fetchExisting(objectID)
}
@@ -59,7 +59,7 @@ public extension CoreStore {
- parameter objects: an array of `DynamicObject`s created/fetched outside the `DataStack`
- returns: the `DynamicObject` array for objects that exists in the `DataStack`
*/
public static func fetchExisting<T: DynamicObject, S: Sequence>(_ objects: S) -> [T] where S.Iterator.Element == T {
public static func fetchExisting<D: DynamicObject, S: Sequence>(_ objects: S) -> [D] where S.Iterator.Element == D {
return self.defaultStack.fetchExisting(objects)
}
@@ -70,7 +70,7 @@ public extension CoreStore {
- parameter objectIDs: the `NSManagedObjectID` array for the objects
- returns: the `DynamicObject` array for objects that exists in the `DataStack`
*/
public static func fetchExisting<T: DynamicObject, S: Sequence>(_ objectIDs: S) -> [T] where S.Iterator.Element == NSManagedObjectID {
public static func fetchExisting<D: DynamicObject, S: Sequence>(_ objectIDs: S) -> [D] where S.Iterator.Element == NSManagedObjectID {
return self.defaultStack.fetchExisting(objectIDs)
}
@@ -82,7 +82,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the first `DynamicObject` instance that satisfies the specified `FetchClause`s
*/
public static func fetchOne<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> T? {
public static func fetchOne<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> D? {
return self.defaultStack.fetchOne(from, fetchClauses)
}
@@ -94,7 +94,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the first `DynamicObject` instance that satisfies the specified `FetchClause`s
*/
public static func fetchOne<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
public static func fetchOne<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> D? {
return self.defaultStack.fetchOne(from, fetchClauses)
}
@@ -106,7 +106,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: all `DynamicObject` instances that satisfy the specified `FetchClause`s
*/
public static func fetchAll<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [T]? {
public static func fetchAll<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> [D]? {
return self.defaultStack.fetchAll(from, fetchClauses)
}
@@ -118,7 +118,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: all `DynamicObject` instances that satisfy the specified `FetchClause`s
*/
public static func fetchAll<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [T]? {
public static func fetchAll<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> [D]? {
return self.defaultStack.fetchAll(from, fetchClauses)
}
@@ -130,7 +130,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the number `DynamicObject`s that satisfy the specified `FetchClause`s
*/
public static func fetchCount<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> Int? {
public static func fetchCount<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> Int? {
return self.defaultStack.fetchCount(from, fetchClauses)
}
@@ -142,7 +142,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the number `DynamicObject`s that satisfy the specified `FetchClause`s
*/
public static func fetchCount<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> Int? {
public static func fetchCount<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> Int? {
return self.defaultStack.fetchCount(from, fetchClauses)
}
@@ -154,7 +154,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s
*/
public static func fetchObjectID<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
public static func fetchObjectID<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
return self.defaultStack.fetchObjectID(from, fetchClauses)
}
@@ -166,7 +166,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s
*/
public static func fetchObjectID<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
public static func fetchObjectID<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
return self.defaultStack.fetchObjectID(from, fetchClauses)
}
@@ -178,7 +178,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s
*/
public static func fetchObjectIDs<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
public static func fetchObjectIDs<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
return self.defaultStack.fetchObjectIDs(from, fetchClauses)
}
@@ -190,7 +190,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s
*/
public static func fetchObjectIDs<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
public static func fetchObjectIDs<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
return self.defaultStack.fetchObjectIDs(from, fetchClauses)
}
@@ -205,7 +205,7 @@ public extension CoreStore {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
public static func queryValue<T, U: QueryableAttributeType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
public static func queryValue<D, U: QueryableAttributeType>(_ from: From<D>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
return self.defaultStack.queryValue(from, selectClause, queryClauses)
}
@@ -220,7 +220,7 @@ public extension CoreStore {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
public static func queryValue<T, U: QueryableAttributeType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
public static func queryValue<D, U: QueryableAttributeType>(_ from: From<D>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
return self.defaultStack.queryValue(from, selectClause, queryClauses)
}
@@ -235,7 +235,7 @@ public extension CoreStore {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
public static func queryAttributes<T>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]? {
public static func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]? {
return self.defaultStack.queryAttributes(from, selectClause, queryClauses)
}
@@ -250,7 +250,7 @@ public extension CoreStore {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
public static func queryAttributes<T>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]? {
public static func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]? {
return self.defaultStack.queryAttributes(from, selectClause, queryClauses)
}

View File

@@ -35,7 +35,7 @@ internal final class CoreStoreFetchedResultsController: NSFetchedResultsControll
// MARK: Internal
@nonobjc
internal convenience init<T>(dataStack: DataStack, fetchRequest: NSFetchRequest<NSManagedObject>, from: From<T>, sectionBy: SectionBy? = nil, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void) {
internal convenience init<D>(dataStack: DataStack, fetchRequest: NSFetchRequest<NSManagedObject>, from: From<D>, sectionBy: SectionBy? = nil, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void) {
self.init(
context: dataStack.mainContext,
@@ -47,7 +47,7 @@ internal final class CoreStoreFetchedResultsController: NSFetchedResultsControll
}
@nonobjc
internal init<T>(context: NSManagedObjectContext, fetchRequest: NSFetchRequest<NSManagedObject>, from: From<T>, sectionBy: SectionBy? = nil, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void) {
internal init<D>(context: NSManagedObjectContext, fetchRequest: NSFetchRequest<NSManagedObject>, from: From<D>, sectionBy: SectionBy? = nil, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void) {
_ = from.applyToFetchRequest(
fetchRequest,

View File

@@ -26,6 +26,58 @@
import CoreData
import Foundation
//public extension From where D: NSManagedObject {
//
//// public func select<R>(_ resultType: R.Type, _ selectTerm: SelectTerm, _ selectTerms: SelectTerm...) -> QueryChainBuilder<D, R> {
////
//// return self.select(resultType, [selectTerm] + selectTerms)
//// }
////
//// public func select<R>(_ resultType: R.Type, _ selectTerms: [SelectTerm]) -> QueryChainBuilder<D, R> {
////
//// return .init(
//// from: self,
//// select: .init(selectTerms),
//// groupBy: .init(),
//// fetchClauses: []
//// )
//// }
//
// public func sectionBy<T>(_ sectionKeyPath: KeyPath<D, T>) -> SectionMonitorChainBuilder<D> {
//
// return self.sectionBy(sectionKeyPath._kvcKeyPathString!, { $0 })
// }
//
// public func sectionBy<T>(_ sectionKeyPath: KeyPath<D, T>, _ sectionIndexTransformer: @escaping (_ sectionName: String?) -> String?) -> SectionMonitorChainBuilder<D> {
//
// return self.sectionBy(sectionKeyPath._kvcKeyPathString!, sectionIndexTransformer)
// }
//
// public func `where`<T>(_ keyPath: KeyPath<D, T>, isEqualTo value: Void?) -> FetchChainBuilder<D> {
//
// return self.where(keyPath._kvcKeyPathString!, isEqualTo: value)
// }
//
// public func `where`<U: QueryableAttributeType>(_ keyPath: KeyPath<D, U>, isEqualTo value: U?) -> FetchChainBuilder<D> {
//
// return self.where(keyPath._kvcKeyPathString!, isEqualTo: value)
// }
//
// public func `where`(_ keyPath: KeyPath<D, D>, isEqualTo object: D?) -> FetchChainBuilder<D> {
//
// return self.where(keyPath._kvcKeyPathString!, isEqualTo: object)
// }
//
// public func `where`<S: Sequence>(_ keyPath: KeyPath<D, S.Iterator.Element>, isMemberOf list: S) -> FetchChainBuilder<D> where S.Iterator.Element: QueryableAttributeType {
//
// return self.where(keyPath._kvcKeyPathString!, isMemberOf: list)
// }
//
// public func `where`<S: Sequence>(_ keyPath: KeyPath<D, S.Iterator.Element>, isMemberOf list: S) -> FetchChainBuilder<D> where S.Iterator.Element == D {
//
// return self.where(keyPath._kvcKeyPathString!, isMemberOf: list)
// }
//}
// MARK: - DynamicObject
@@ -92,7 +144,7 @@ public extension DynamicObject where Self: CoreStoreObject {
let person = CoreStore.fetchOne(From<Person>(), Person.where { $0.nickname == "John" })
```
*/
public static func `where`(_ condition: (Self) -> Where) -> Where {
public static func `where`(_ condition: (Self) -> Where<Self>) -> Where<Self> {
return condition(self.meta)
}
@@ -103,7 +155,7 @@ public extension DynamicObject where Self: CoreStoreObject {
let person = CoreStore.fetchAll(From<Person>(), Person.orderBy(ascending: { $0.age }))
```
*/
public static func orderBy<O, V>(ascending attribute: (Self) -> ValueContainer<O>.Required<V>) -> OrderBy {
public static func orderBy<O, V>(ascending attribute: (Self) -> ValueContainer<O>.Required<V>) -> OrderBy<Self> {
return OrderBy(.ascending(attribute(self.meta).keyPath))
}
@@ -114,7 +166,7 @@ public extension DynamicObject where Self: CoreStoreObject {
let person = CoreStore.fetchAll(From<Person>(), Person.orderBy(ascending: { $0.age }))
```
*/
public static func orderBy<O, V>(ascending attribute: (Self) -> ValueContainer<O>.Optional<V>) -> OrderBy {
public static func orderBy<O, V>(ascending attribute: (Self) -> ValueContainer<O>.Optional<V>) -> OrderBy<Self> {
return OrderBy(.ascending(attribute(self.meta).keyPath))
}
@@ -125,7 +177,7 @@ public extension DynamicObject where Self: CoreStoreObject {
let person = CoreStore.fetchAll(From<Person>(), Person.orderBy(descending: { $0.age }))
```
*/
public static func orderBy<O, V>(descending attribute: (Self) -> ValueContainer<O>.Required<V>) -> OrderBy {
public static func orderBy<O, V>(descending attribute: (Self) -> ValueContainer<O>.Required<V>) -> OrderBy<Self> {
return OrderBy(.descending(attribute(self.meta).keyPath))
}
@@ -136,22 +188,7 @@ public extension DynamicObject where Self: CoreStoreObject {
let person = CoreStore.fetchAll(From<Person>(), Person.orderBy(descending: { $0.age }))
```
*/
public static func orderBy<O, V>(descending attribute: (Self) -> ValueContainer<O>.Optional<V>) -> OrderBy {
return OrderBy(.descending(attribute(self.meta).keyPath))
}
// MARK: Deprecated
@available(*, deprecated, renamed: "orderBy(ascending:)")
public static func ascending<O, V>(_ attribute: (Self) -> ValueContainer<O>.Optional<V>) -> OrderBy {
return OrderBy(.ascending(attribute(self.meta).keyPath))
}
@available(*, deprecated, renamed: "orderBy(descending:)")
public static func descending<O, V>(_ attribute: (Self) -> ValueContainer<O>.Optional<V>) -> OrderBy {
public static func orderBy<O, V>(descending attribute: (Self) -> ValueContainer<O>.Optional<V>) -> OrderBy<Self> {
return OrderBy(.descending(attribute(self.meta).keyPath))
}
@@ -169,7 +206,7 @@ public extension ValueContainer.Required {
```
*/
@inline(__always)
public static func == (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where {
public static func == (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where<O> {
return Where(attribute.keyPath, isEqualTo: value)
}
@@ -181,7 +218,7 @@ public extension ValueContainer.Required {
```
*/
@inline(__always)
public static func != (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where {
public static func != (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where<O> {
return !Where(attribute.keyPath, isEqualTo: value)
}
@@ -193,7 +230,7 @@ public extension ValueContainer.Required {
```
*/
@inline(__always)
public static func < (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where {
public static func < (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where<O> {
return Where("%K < %@", attribute.keyPath, value)
}
@@ -205,7 +242,7 @@ public extension ValueContainer.Required {
```
*/
@inline(__always)
public static func > (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where {
public static func > (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where<O> {
return Where("%K > %@", attribute.keyPath, value)
}
@@ -217,7 +254,7 @@ public extension ValueContainer.Required {
```
*/
@inline(__always)
public static func <= (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where {
public static func <= (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where<O> {
return Where("%K <= %@", attribute.keyPath, value)
}
@@ -229,7 +266,7 @@ public extension ValueContainer.Required {
```
*/
@inline(__always)
public static func >= (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where {
public static func >= (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where<O> {
return Where("%K >= %@", attribute.keyPath, value)
}
@@ -241,7 +278,7 @@ public extension ValueContainer.Required {
```
*/
@inline(__always)
public static func ~= <S: Sequence>(_ sequence: S, _ attribute: ValueContainer<O>.Required<V>) -> Where where S.Iterator.Element == V {
public static func ~= <S: Sequence>(_ sequence: S, _ attribute: ValueContainer<O>.Required<V>) -> Where<O> where S.Iterator.Element == V {
return Where(attribute.keyPath, isMemberOf: sequence)
}
@@ -259,7 +296,7 @@ public extension ValueContainer.Optional {
```
*/
@inline(__always)
public static func == (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where {
public static func == (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where<O> {
return Where(attribute.keyPath, isEqualTo: value)
}
@@ -271,7 +308,7 @@ public extension ValueContainer.Optional {
```
*/
@inline(__always)
public static func != (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where {
public static func != (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where<O> {
return !Where(attribute.keyPath, isEqualTo: value)
}
@@ -283,7 +320,7 @@ public extension ValueContainer.Optional {
```
*/
@inline(__always)
public static func < (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where {
public static func < (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where<O> {
if let value = value {
@@ -302,7 +339,7 @@ public extension ValueContainer.Optional {
```
*/
@inline(__always)
public static func > (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where {
public static func > (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where<O> {
if let value = value {
@@ -321,7 +358,7 @@ public extension ValueContainer.Optional {
```
*/
@inline(__always)
public static func <= (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where {
public static func <= (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where<O> {
if let value = value {
@@ -340,7 +377,7 @@ public extension ValueContainer.Optional {
```
*/
@inline(__always)
public static func >= (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where {
public static func >= (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where<O> {
if let value = value {
@@ -359,7 +396,7 @@ public extension ValueContainer.Optional {
```
*/
@inline(__always)
public static func ~= <S: Sequence>(_ sequence: S, _ attribute: ValueContainer<O>.Optional<V>) -> Where where S.Iterator.Element == V {
public static func ~= <S: Sequence>(_ sequence: S, _ attribute: ValueContainer<O>.Optional<V>) -> Where<O> where S.Iterator.Element == V {
return Where(attribute.keyPath, isMemberOf: sequence)
}
@@ -377,7 +414,7 @@ public extension RelationshipContainer.ToOne {
```
*/
@inline(__always)
public static func == (_ relationship: RelationshipContainer<O>.ToOne<D>, _ object: D?) -> Where {
public static func == (_ relationship: RelationshipContainer<O>.ToOne<D>, _ object: D?) -> Where<O> {
return Where(relationship.keyPath, isEqualTo: object)
}
@@ -389,7 +426,7 @@ public extension RelationshipContainer.ToOne {
```
*/
@inline(__always)
public static func != (_ relationship: RelationshipContainer<O>.ToOne<D>, _ object: D?) -> Where {
public static func != (_ relationship: RelationshipContainer<O>.ToOne<D>, _ object: D?) -> Where<O> {
return !Where(relationship.keyPath, isEqualTo: object)
}
@@ -401,7 +438,7 @@ public extension RelationshipContainer.ToOne {
```
*/
@inline(__always)
public static func ~= (_ relationship: RelationshipContainer<O>.ToOne<D>, _ object: D?) -> Where {
public static func ~= (_ relationship: RelationshipContainer<O>.ToOne<D>, _ object: D?) -> Where<O> {
return Where(relationship.keyPath, isEqualTo: object)
}
@@ -413,7 +450,7 @@ public extension RelationshipContainer.ToOne {
```
*/
@inline(__always)
public static func ~= <S: Sequence>(_ sequence: S, _ relationship: RelationshipContainer<O>.ToOne<D>) -> Where where S.Iterator.Element == D {
public static func ~= <S: Sequence>(_ sequence: S, _ relationship: RelationshipContainer<O>.ToOne<D>) -> Where<O> where S.Iterator.Element == D {
return Where(relationship.keyPath, isMemberOf: sequence)
}

View File

@@ -38,7 +38,7 @@ public extension DataStack {
- parameter object: the `DynamicObject` to observe changes from
- returns: a `ObjectMonitor` that monitors changes to `object`
*/
public func monitorObject<T>(_ object: T) -> ObjectMonitor<T> {
public func monitorObject<D>(_ object: D) -> ObjectMonitor<D> {
CoreStore.assert(
Thread.isMainThread,
@@ -54,7 +54,7 @@ public extension DataStack {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list
*/
public func monitorList<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
public func monitorList<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> ListMonitor<D> {
return self.monitorList(from, fetchClauses)
}
@@ -66,7 +66,7 @@ public extension DataStack {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list
*/
public func monitorList<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
public func monitorList<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> ListMonitor<D> {
CoreStore.assert(
Thread.isMainThread,
@@ -82,7 +82,7 @@ public extension DataStack {
CoreStore.assert(
fetchRequest.sortDescriptors?.isEmpty == false,
"An \(cs_typeName(ListMonitor<T>.self)) requires a sort information. Specify from a \(cs_typeName(OrderBy.self)) clause or any custom \(cs_typeName(FetchClause.self)) that provides a sort descriptor."
"An \(cs_typeName(ListMonitor<D>.self)) requires a sort information. Specify from a \(cs_typeName(OrderBy<D>.self)) clause or any custom \(cs_typeName(FetchClause.self)) that provides a sort descriptor."
)
}
)
@@ -95,7 +95,7 @@ public extension DataStack {
- parameter from: a `From` clause indicating the entity type
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/
public func monitorList<T>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) {
public func monitorList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ fetchClauses: FetchClause...) {
self.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses)
}
@@ -107,7 +107,7 @@ public extension DataStack {
- parameter from: a `From` clause indicating the entity type
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/
public func monitorList<T>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) {
public func monitorList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ fetchClauses: [FetchClause]) {
CoreStore.assert(
Thread.isMainThread,
@@ -123,7 +123,7 @@ public extension DataStack {
CoreStore.assert(
fetchRequest.sortDescriptors?.isEmpty == false,
"An \(cs_typeName(ListMonitor<T>.self)) requires a sort information. Specify from a \(cs_typeName(OrderBy.self)) clause or any custom \(cs_typeName(FetchClause.self)) that provides a sort descriptor."
"An \(cs_typeName(ListMonitor<D>.self)) requires a sort information. Specify from a \(cs_typeName(OrderBy<D>.self)) clause or any custom \(cs_typeName(FetchClause.self)) that provides a sort descriptor."
)
},
createAsynchronously: createAsynchronously
@@ -138,7 +138,7 @@ public extension DataStack {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list
*/
public func monitorSectionedList<T>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
public func monitorSectionedList<D>(_ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<D> {
return self.monitorSectionedList(from, sectionBy, fetchClauses)
}
@@ -151,7 +151,7 @@ public extension DataStack {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list
*/
public func monitorSectionedList<T>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
public func monitorSectionedList<D>(_ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<D> {
CoreStore.assert(
Thread.isMainThread,
@@ -168,7 +168,7 @@ public extension DataStack {
CoreStore.assert(
fetchRequest.sortDescriptors?.isEmpty == false,
"An \(cs_typeName(ListMonitor<T>.self)) requires a sort information. Specify from a \(cs_typeName(OrderBy.self)) clause or any custom \(cs_typeName(FetchClause.self)) that provides a sort descriptor."
"An \(cs_typeName(ListMonitor<D>.self)) requires a sort information. Specify from a \(cs_typeName(OrderBy<D>.self)) clause or any custom \(cs_typeName(FetchClause.self)) that provides a sort descriptor."
)
}
)
@@ -182,7 +182,7 @@ public extension DataStack {
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/
public func monitorSectionedList<T>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
public func monitorSectionedList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
self.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses)
}
@@ -195,7 +195,7 @@ public extension DataStack {
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/
public func monitorSectionedList<T>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
public func monitorSectionedList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
CoreStore.assert(
Thread.isMainThread,
@@ -212,7 +212,7 @@ public extension DataStack {
CoreStore.assert(
fetchRequest.sortDescriptors?.isEmpty == false,
"An \(cs_typeName(ListMonitor<T>.self)) requires a sort information. Specify from a \(cs_typeName(OrderBy.self)) clause or any custom \(cs_typeName(FetchClause.self)) that provides a sort descriptor."
"An \(cs_typeName(ListMonitor<D>.self)) requires a sort information. Specify from a \(cs_typeName(OrderBy<D>.self)) clause or any custom \(cs_typeName(FetchClause.self)) that provides a sort descriptor."
)
},
createAsynchronously: createAsynchronously

View File

@@ -39,7 +39,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter object: a reference to the object created/fetched outside the `DataStack`
- returns: the `DynamicObject` instance if the object exists in the `DataStack`, or `nil` if not found.
*/
public func fetchExisting<T: DynamicObject>(_ object: T) -> T? {
public func fetchExisting<D: DynamicObject>(_ object: D) -> D? {
return self.mainContext.fetchExisting(object)
}
@@ -50,7 +50,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter objectID: the `NSManagedObjectID` for the object
- returns: the `DynamicObject` instance if the object exists in the `DataStack`, or `nil` if not found.
*/
public func fetchExisting<T: DynamicObject>(_ objectID: NSManagedObjectID) -> T? {
public func fetchExisting<D: DynamicObject>(_ objectID: NSManagedObjectID) -> D? {
return self.mainContext.fetchExisting(objectID)
}
@@ -61,7 +61,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter objects: an array of `DynamicObject`s created/fetched outside the `DataStack`
- returns: the `DynamicObject` array for objects that exists in the `DataStack`
*/
public func fetchExisting<T: DynamicObject, S: Sequence>(_ objects: S) -> [T] where S.Iterator.Element == T {
public func fetchExisting<D: DynamicObject, S: Sequence>(_ objects: S) -> [D] where S.Iterator.Element == D {
return self.mainContext.fetchExisting(objects)
}
@@ -72,7 +72,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter objectIDs: the `NSManagedObjectID` array for the objects
- returns: the `DynamicObject` array for objects that exists in the `DataStack`
*/
public func fetchExisting<T: DynamicObject, S: Sequence>(_ objectIDs: S) -> [T] where S.Iterator.Element == NSManagedObjectID {
public func fetchExisting<D: DynamicObject, S: Sequence>(_ objectIDs: S) -> [D] where S.Iterator.Element == NSManagedObjectID {
return self.mainContext.fetchExisting(objectIDs)
}
@@ -84,7 +84,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the first `DynamicObject` instance that satisfies the specified `FetchClause`s
*/
public func fetchOne<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> T? {
public func fetchOne<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> D? {
CoreStore.assert(
Thread.isMainThread,
@@ -100,7 +100,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the first `DynamicObject` instance that satisfies the specified `FetchClause`s
*/
public func fetchOne<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
public func fetchOne<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> D? {
CoreStore.assert(
Thread.isMainThread,
@@ -109,6 +109,16 @@ extension DataStack: FetchableSource, QueryableSource {
return self.mainContext.fetchOne(from, fetchClauses)
}
// TODO: docs
public func fetchOne<B: FetchChainableBuilderType>(_ clauseChain: B) -> B.ObjectType? {
CoreStore.assert(
Thread.isMainThread,
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
)
return self.mainContext.fetchOne(clauseChain)
}
/**
Fetches all `DynamicObject` instances that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -116,7 +126,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: all `DynamicObject` instances that satisfy the specified `FetchClause`s
*/
public func fetchAll<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [T]? {
public func fetchAll<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> [D]? {
CoreStore.assert(
Thread.isMainThread,
@@ -132,7 +142,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: all `DynamicObject` instances that satisfy the specified `FetchClause`s
*/
public func fetchAll<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [T]? {
public func fetchAll<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> [D]? {
CoreStore.assert(
Thread.isMainThread,
@@ -141,6 +151,16 @@ extension DataStack: FetchableSource, QueryableSource {
return self.mainContext.fetchAll(from, fetchClauses)
}
// TODO: docs
public func fetchAll<B: FetchChainableBuilderType>(_ clauseChain: B) -> [B.ObjectType]? {
CoreStore.assert(
Thread.isMainThread,
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
)
return self.mainContext.fetchAll(clauseChain)
}
/**
Fetches the number of `DynamicObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -148,7 +168,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the number `DynamicObject`s that satisfy the specified `FetchClause`s
*/
public func fetchCount<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> Int? {
public func fetchCount<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> Int? {
CoreStore.assert(
Thread.isMainThread,
@@ -164,7 +184,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the number `DynamicObject`s that satisfy the specified `FetchClause`s
*/
public func fetchCount<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> Int? {
public func fetchCount<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> Int? {
CoreStore.assert(
Thread.isMainThread,
@@ -173,6 +193,16 @@ extension DataStack: FetchableSource, QueryableSource {
return self.mainContext.fetchCount(from, fetchClauses)
}
// TODO: docs
public func fetchCount<B: FetchChainableBuilderType>(_ clauseChain: B) -> Int? {
CoreStore.assert(
Thread.isMainThread,
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
)
return self.mainContext.fetchCount(clauseChain)
}
/**
Fetches the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -180,7 +210,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s
*/
public func fetchObjectID<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
public func fetchObjectID<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
CoreStore.assert(
Thread.isMainThread,
@@ -196,7 +226,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s
*/
public func fetchObjectID<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
public func fetchObjectID<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
CoreStore.assert(
Thread.isMainThread,
@@ -205,6 +235,16 @@ extension DataStack: FetchableSource, QueryableSource {
return self.mainContext.fetchObjectID(from, fetchClauses)
}
// TODO: docs
public func fetchObjectID<B: FetchChainableBuilderType>(_ clauseChain: B) -> NSManagedObjectID? {
CoreStore.assert(
Thread.isMainThread,
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
)
return self.mainContext.fetchObjectID(clauseChain)
}
/**
Fetches the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -212,7 +252,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s
*/
public func fetchObjectIDs<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
public func fetchObjectIDs<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
CoreStore.assert(
Thread.isMainThread,
@@ -228,7 +268,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s
*/
public func fetchObjectIDs<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
public func fetchObjectIDs<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
CoreStore.assert(
Thread.isMainThread,
@@ -237,6 +277,16 @@ extension DataStack: FetchableSource, QueryableSource {
return self.mainContext.fetchObjectIDs(from, fetchClauses)
}
// TODO: docs
public func fetchObjectIDs<B: FetchChainableBuilderType>(_ clauseChain: B) -> [NSManagedObjectID]? {
CoreStore.assert(
Thread.isMainThread,
"Attempted to fetch from a \(cs_typeName(self)) outside the main thread."
)
return self.mainContext.fetchObjectIDs(clauseChain)
}
// MARK: QueryableSource
@@ -250,7 +300,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
public func queryValue<T, U: QueryableAttributeType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
public func queryValue<D, U: QueryableAttributeType>(_ from: From<D>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
CoreStore.assert(
Thread.isMainThread,
@@ -269,7 +319,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
public func queryValue<T, U: QueryableAttributeType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
public func queryValue<D, U: QueryableAttributeType>(_ from: From<D>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
CoreStore.assert(
Thread.isMainThread,
@@ -288,7 +338,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
public func queryAttributes<T>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]? {
public func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]? {
CoreStore.assert(
Thread.isMainThread,
@@ -307,7 +357,7 @@ extension DataStack: FetchableSource, QueryableSource {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
public func queryAttributes<T>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]? {
public func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]? {
CoreStore.assert(
Thread.isMainThread,

View File

@@ -79,9 +79,9 @@ extension NSManagedObject: DynamicObject {
public class func cs_fromRaw(object: NSManagedObject) -> Self {
@inline(__always)
func forceCast<T: NSManagedObject>(_ value: Any) -> T {
func forceCast<D: NSManagedObject>(_ value: Any) -> D {
return value as! T
return value as! D
}
return forceCast(object)
}
@@ -125,9 +125,9 @@ extension CoreStoreObject {
if let coreStoreObject = object.coreStoreObject {
@inline(__always)
func forceCast<T: CoreStoreObject>(_ value: CoreStoreObject) -> T {
func forceCast<D: CoreStoreObject>(_ value: CoreStoreObject) -> D {
return value as! T
return value as! D
}
return forceCast(coreStoreObject)
}

View File

@@ -1,143 +0,0 @@
//
// FetchCondition.swift
// CoreStore
//
// Copyright © 2017 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
import CoreData
public struct ChainedClauseBuilder<T: DynamicObject> {
public let from: From<T>
public let fetchClauses: [FetchClause] = []
internal init(from: From<T>) {
self.from = from
}
}
extension From: ClauseChain {
public typealias ObjectType = T
public typealias TraitType = FetchTrait
public var builder: ChainedClauseBuilder<T> {
return .init(from: self)
}
}
public struct ChainedWhere<D: DynamicObject, T: ClauseTrait>: ClauseChain {
public typealias ObjectType = D
public typealias TraitType = T
public let builder: ChainedClauseBuilder<ObjectType>
fileprivate init(builder: ChainedClauseBuilder<ObjectType>) {
var newBuilder = builder
// newBuilder.fetchClauses.append(Where())
self.builder = newBuilder
}
}
public struct ChainedOrderBy<D: DynamicObject, T: ClauseTrait>: ClauseChain {
public typealias ObjectType = D
public typealias TraitType = T
public let builder: ChainedClauseBuilder<ObjectType>
fileprivate init(builder: ChainedClauseBuilder<ObjectType>) {
var newBuilder = builder
// newBuilder.fetchClauses.append(Where())
self.builder = newBuilder
}
}
public struct ChainedSelect<D: DynamicObject, T: ClauseTrait>: ClauseChain {
public typealias ObjectType = D
public typealias TraitType = T
public let builder: ChainedClauseBuilder<ObjectType>
fileprivate init(builder: ChainedClauseBuilder<ObjectType>) {
var newBuilder = builder
// newBuilder.fetchClauses.append(Where())
self.builder = newBuilder
}
}
public protocol ClauseTrait {}
public enum FetchTrait: ClauseTrait {}
public enum QueryTrait: ClauseTrait {}
public enum SectionTrait: ClauseTrait {}
public protocol ClauseChain {
associatedtype ObjectType: DynamicObject
associatedtype TraitType: ClauseTrait
var builder: ChainedClauseBuilder<ObjectType> { get }
}
public extension ClauseChain where Self.TraitType == FetchTrait {
public func `where`() -> ChainedWhere<ObjectType, FetchTrait> {
return .init(builder: self.builder)
}
public func orderBy() -> ChainedOrderBy<ObjectType, FetchTrait> {
return .init(builder: self.builder)
}
public func select() -> ChainedSelect<ObjectType, QueryTrait> {
return .init(builder: self.builder)
}
}
public extension ClauseChain where Self.TraitType == QueryTrait {
public func `where`() -> ChainedWhere<ObjectType, QueryTrait> {
return .init(builder: self.builder)
}
public func orderBy() -> ChainedOrderBy<ObjectType, QueryTrait> {
return .init(builder: self.builder)
}
}

View File

@@ -40,7 +40,7 @@ public protocol FetchableSource: class {
- parameter object: a reference to the object created/fetched outside the `FetchableSource`'s context
- returns: the `DynamicObject` instance if the object exists in the `FetchableSource`'s context, or `nil` if not found.
*/
func fetchExisting<T: DynamicObject>(_ object: T) -> T?
func fetchExisting<D: DynamicObject>(_ object: D) -> D?
/**
Fetches the `DynamicObject` instance in the `FetchableSource`'s context from an `NSManagedObjectID`.
@@ -48,7 +48,7 @@ public protocol FetchableSource: class {
- parameter objectID: the `NSManagedObjectID` for the object
- returns: the `DynamicObject` instance if the object exists in the `FetchableSource`, or `nil` if not found.
*/
func fetchExisting<T: DynamicObject>(_ objectID: NSManagedObjectID) -> T?
func fetchExisting<D: DynamicObject>(_ objectID: NSManagedObjectID) -> D?
/**
Fetches the `DynamicObject` instances in the `FetchableSource`'s context from references created from another managed object context.
@@ -56,7 +56,7 @@ public protocol FetchableSource: class {
- parameter objects: an array of `DynamicObject`s created/fetched outside the `FetchableSource`'s context
- returns: the `DynamicObject` array for objects that exists in the `FetchableSource`
*/
func fetchExisting<T: DynamicObject, S: Sequence>(_ objects: S) -> [T] where S.Iterator.Element == T
func fetchExisting<D: DynamicObject, S: Sequence>(_ objects: S) -> [D] where S.Iterator.Element == D
/**
Fetches the `DynamicObject` instances in the `FetchableSource`'s context from a list of `NSManagedObjectID`.
@@ -64,7 +64,7 @@ public protocol FetchableSource: class {
- parameter objectIDs: the `NSManagedObjectID` array for the objects
- returns: the `DynamicObject` array for objects that exists in the `FetchableSource`'s context
*/
func fetchExisting<T: DynamicObject, S: Sequence>(_ objectIDs: S) -> [T] where S.Iterator.Element == NSManagedObjectID
func fetchExisting<D: DynamicObject, S: Sequence>(_ objectIDs: S) -> [D] where S.Iterator.Element == NSManagedObjectID
/**
Fetches the first `DynamicObject` instance that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -73,7 +73,7 @@ public protocol FetchableSource: class {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the first `DynamicObject` instance that satisfies the specified `FetchClause`s
*/
func fetchOne<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> T?
func fetchOne<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> D?
/**
Fetches the first `DynamicObject` instance that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -82,7 +82,10 @@ public protocol FetchableSource: class {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the first `DynamicObject` instance that satisfies the specified `FetchClause`s
*/
func fetchOne<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> T?
func fetchOne<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> D?
// TODO: docs
func fetchOne<B: FetchChainableBuilderType>(_ clauseChain: B) -> B.ObjectType?
/**
Fetches all `DynamicObject` instances that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -91,7 +94,7 @@ public protocol FetchableSource: class {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: all `DynamicObject` instances that satisfy the specified `FetchClause`s
*/
func fetchAll<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [T]?
func fetchAll<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> [D]?
/**
Fetches all `DynamicObject` instances that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -100,7 +103,10 @@ public protocol FetchableSource: class {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: all `DynamicObject` instances that satisfy the specified `FetchClause`s
*/
func fetchAll<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [T]?
func fetchAll<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> [D]?
// TODO: docs
func fetchAll<B: FetchChainableBuilderType>(_ clauseChain: B) -> [B.ObjectType]?
/**
Fetches the number of `DynamicObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -109,7 +115,7 @@ public protocol FetchableSource: class {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the number `DynamicObject`s that satisfy the specified `FetchClause`s
*/
func fetchCount<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> Int?
func fetchCount<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> Int?
/**
Fetches the number of `DynamicObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -118,7 +124,10 @@ public protocol FetchableSource: class {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the number `DynamicObject`s that satisfy the specified `FetchClause`s
*/
func fetchCount<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> Int?
func fetchCount<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> Int?
// TODO: docs
func fetchCount<B: FetchChainableBuilderType>(_ clauseChain: B) -> Int?
/**
Fetches the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -127,7 +136,7 @@ public protocol FetchableSource: class {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s
*/
func fetchObjectID<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID?
func fetchObjectID<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> NSManagedObjectID?
/**
Fetches the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -136,7 +145,10 @@ public protocol FetchableSource: class {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s
*/
func fetchObjectID<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID?
func fetchObjectID<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID?
// TODO: docs
func fetchObjectID<B: FetchChainableBuilderType>(_ clauseChain: B) -> NSManagedObjectID?
/**
Fetches the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -145,7 +157,7 @@ public protocol FetchableSource: class {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s
*/
func fetchObjectIDs<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]?
func fetchObjectIDs<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]?
/**
Fetches the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
@@ -154,7 +166,10 @@ public protocol FetchableSource: class {
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s
*/
func fetchObjectIDs<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]?
func fetchObjectIDs<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]?
// TODO: docs
func fetchObjectIDs<B: FetchChainableBuilderType>(_ clauseChain: B) -> [NSManagedObjectID]?
/**
The internal `NSManagedObjectContext` managed by this `FetchableSource`. Using this context directly should typically be avoided, and is provided by CoreStore only for extremely specialized cases.

View File

@@ -39,12 +39,12 @@ import CoreData
let person = transaction.fetchOne(From<Person>("Configuration1"))
```
*/
public struct From<T: DynamicObject> {
public struct From<D: DynamicObject> {
/**
The associated `NSManagedObject` or `CoreStoreObject` entity class
*/
public let entityClass: T.Type
public let entityClass: D.Type
/**
The `NSPersistentStore` configuration names to associate objects from.
@@ -60,7 +60,7 @@ public struct From<T: DynamicObject> {
*/
public init() {
self.init(entityClass: T.self, configurations: nil)
self.init(entityClass: D.self, configurations: nil)
}
/**
@@ -70,7 +70,7 @@ public struct From<T: DynamicObject> {
```
- parameter entity: the associated `NSManagedObject` or `CoreStoreObject` type
*/
public init(_ entity: T.Type) {
public init(_ entity: D.Type) {
self.init(entityClass: entity, configurations: nil)
}
@@ -85,7 +85,7 @@ public struct From<T: DynamicObject> {
*/
public init(_ configuration: ModelConfiguration, _ otherConfigurations: ModelConfiguration...) {
self.init(entityClass: T.self, configurations: [configuration] + otherConfigurations)
self.init(entityClass: D.self, configurations: [configuration] + otherConfigurations)
}
/**
@@ -97,7 +97,7 @@ public struct From<T: DynamicObject> {
*/
public init(_ configurations: [ModelConfiguration]) {
self.init(entityClass: T.self, configurations: configurations)
self.init(entityClass: D.self, configurations: configurations)
}
/**
@@ -109,7 +109,7 @@ public struct From<T: DynamicObject> {
- parameter configuration: the `NSPersistentStore` configuration name to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject` or `CoreStoreObject`'s entity type. Set to `nil` to use the default configuration.
- parameter otherConfigurations: an optional list of other configuration names to associate objects from (see `configuration` parameter)
*/
public init(_ entity: T.Type, _ configuration: ModelConfiguration, _ otherConfigurations: ModelConfiguration...) {
public init(_ entity: D.Type, _ configuration: ModelConfiguration, _ otherConfigurations: ModelConfiguration...) {
self.init(entityClass: entity, configurations: [configuration] + otherConfigurations)
}
@@ -122,7 +122,7 @@ public struct From<T: DynamicObject> {
- parameter entity: the associated `NSManagedObject` or `CoreStoreObject` type
- parameter configurations: a list of `NSPersistentStore` configuration names to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject` or `CoreStoreObject`'s entity type. Set to `nil` to use the default configuration.
*/
public init(_ entity: T.Type, _ configurations: [ModelConfiguration]) {
public init(_ entity: D.Type, _ configurations: [ModelConfiguration]) {
self.init(entityClass: entity, configurations: configurations)
}
@@ -132,7 +132,7 @@ public struct From<T: DynamicObject> {
internal let findPersistentStores: (_ context: NSManagedObjectContext) -> [NSPersistentStore]?
internal init(entityClass: T.Type, configurations: [ModelConfiguration]?, findPersistentStores: @escaping (_ context: NSManagedObjectContext) -> [NSPersistentStore]?) {
internal init(entityClass: D.Type, configurations: [ModelConfiguration]?, findPersistentStores: @escaping (_ context: NSManagedObjectContext) -> [NSPersistentStore]?) {
self.entityClass = entityClass
self.configurations = configurations
@@ -167,7 +167,7 @@ public struct From<T: DynamicObject> {
// MARK: Private
private init(entityClass: T.Type, configurations: [ModelConfiguration]?) {
private init(entityClass: D.Type, configurations: [ModelConfiguration]?) {
self.entityClass = entityClass
self.configurations = configurations

View File

@@ -39,12 +39,12 @@ import CoreData
let person = transaction.create(Into<MyPersonEntity>("Configuration1"))
```
*/
public struct Into<T: DynamicObject>: Hashable {
public struct Into<D: DynamicObject>: Hashable {
/**
The associated `NSManagedObject` or `CoreStoreObject` entity class
*/
public let entityClass: T.Type
public let entityClass: D.Type
/**
The `NSPersistentStore` configuration name to associate objects from.
@@ -60,7 +60,7 @@ public struct Into<T: DynamicObject>: Hashable {
*/
public init() {
self.init(entityClass: T.self, configuration: nil, inferStoreIfPossible: true)
self.init(entityClass: D.self, configuration: nil, inferStoreIfPossible: true)
}
/**
@@ -70,7 +70,7 @@ public struct Into<T: DynamicObject>: Hashable {
```
- parameter entity: the `NSManagedObject` type to be created
*/
public init(_ entity: T.Type) {
public init(_ entity: D.Type) {
self.init(entityClass: entity, configuration: nil, inferStoreIfPossible: true)
}
@@ -84,7 +84,7 @@ public struct Into<T: DynamicObject>: Hashable {
*/
public init(_ configuration: ModelConfiguration) {
self.init(entityClass: T.self, configuration: configuration, inferStoreIfPossible: false)
self.init(entityClass: D.self, configuration: configuration, inferStoreIfPossible: false)
}
/**
@@ -95,7 +95,7 @@ public struct Into<T: DynamicObject>: Hashable {
- parameter entity: the `NSManagedObject` type to be created
- parameter configuration: the `NSPersistentStore` configuration name to associate the object to. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
*/
public init(_ entity: T.Type, _ configuration: ModelConfiguration) {
public init(_ entity: D.Type, _ configuration: ModelConfiguration) {
self.init(entityClass: entity, configuration: configuration, inferStoreIfPossible: false)
}
@@ -125,7 +125,7 @@ public struct Into<T: DynamicObject>: Hashable {
internal let inferStoreIfPossible: Bool
internal init(entityClass: T.Type, configuration: ModelConfiguration, inferStoreIfPossible: Bool) {
internal init(entityClass: D.Type, configuration: ModelConfiguration, inferStoreIfPossible: Bool) {
self.entityClass = entityClass
self.configuration = configuration

View File

@@ -42,7 +42,7 @@ public extension DataStack {
- returns: an `NSFetchedResultsController` that observes the `DataStack`
*/
@nonobjc
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<T> {
public func createFetchedResultsController<D: NSManagedObject>(_ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<D> {
return createFRC(
fromContext: self.mainContext,
@@ -62,7 +62,7 @@ public extension DataStack {
- returns: an `NSFetchedResultsController` that observes the `DataStack`
*/
@nonobjc
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
public func createFetchedResultsController<D: NSManagedObject>(_ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<D> {
return createFRC(
fromContext: self.mainContext,
@@ -81,7 +81,7 @@ public extension DataStack {
- returns: an `NSFetchedResultsController` that observes the `DataStack`
*/
@nonobjc
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<T> {
public func createFetchedResultsController<D: NSManagedObject>(_ from: From<D>, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<D> {
return createFRC(
fromContext: self.mainContext,
@@ -100,7 +100,7 @@ public extension DataStack {
- returns: an `NSFetchedResultsController` that observes the `DataStack`
*/
@nonobjc
public func createFetchedResultsController<T: NSManagedObject>(forDataStack dataStack: DataStack, _ from: From<T>, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
public func createFetchedResultsController<D: NSManagedObject>(forDataStack dataStack: DataStack, _ from: From<D>, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<D> {
return createFRC(
fromContext: self.mainContext,
@@ -127,7 +127,7 @@ public extension UnsafeDataTransaction {
- returns: an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`
*/
@nonobjc
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<T> {
public func createFetchedResultsController<D: NSManagedObject>(_ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<D> {
return createFRC(
fromContext: self.context,
@@ -147,7 +147,7 @@ public extension UnsafeDataTransaction {
- returns: an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`
*/
@nonobjc
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
public func createFetchedResultsController<D: NSManagedObject>(_ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<D> {
return createFRC(
fromContext: self.context,
@@ -166,7 +166,7 @@ public extension UnsafeDataTransaction {
- returns: an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`
*/
@nonobjc
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<T> {
public func createFetchedResultsController<D: NSManagedObject>(_ from: From<D>, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<D> {
return createFRC(
fromContext: self.context,
@@ -185,7 +185,7 @@ public extension UnsafeDataTransaction {
- returns: an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`
*/
@nonobjc
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
public func createFetchedResultsController<D: NSManagedObject>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<D> {
return createFRC(
fromContext: self.context,
@@ -201,7 +201,7 @@ public extension UnsafeDataTransaction {
// MARK: - Private
@available(OSX 10.12, *)
fileprivate func createFRC<T: NSManagedObject>(fromContext context: NSManagedObjectContext, from: From<T>, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
fileprivate func createFRC<D: NSManagedObject>(fromContext context: NSManagedObjectContext, from: From<D>, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) -> NSFetchedResultsController<D> {
let controller = CoreStoreFetchedResultsController(
context: context,
@@ -214,7 +214,7 @@ fileprivate func createFRC<T: NSManagedObject>(fromContext context: NSManagedObj
CoreStore.assert(
fetchRequest.sortDescriptors?.isEmpty == false,
"An \(cs_typeName(NSFetchedResultsController<NSManagedObject>.self)) requires a sort information. Specify from a \(cs_typeName(OrderBy.self)) clause or any custom \(cs_typeName(FetchClause.self)) that provides a sort descriptor."
"An \(cs_typeName(NSFetchedResultsController<D>.self)) requires a sort information. Specify from a \(cs_typeName(OrderBy<D>.self)) clause or any custom \(cs_typeName(FetchClause.self)) that provides a sort descriptor."
)
}
)

View File

@@ -34,7 +34,7 @@ extension NSManagedObjectContext: FetchableSource, QueryableSource {
// MARK: FetchableSource
@nonobjc
public func fetchExisting<T: DynamicObject>(_ object: T) -> T? {
public func fetchExisting<D: DynamicObject>(_ object: D) -> D? {
let rawObject = object.cs_toRaw()
if rawObject.objectID.isTemporaryID {
@@ -62,7 +62,7 @@ extension NSManagedObjectContext: FetchableSource, QueryableSource {
return object
}
return T.cs_fromRaw(object: existingRawObject)
return D.cs_fromRaw(object: existingRawObject)
}
catch {
@@ -75,12 +75,12 @@ extension NSManagedObjectContext: FetchableSource, QueryableSource {
}
@nonobjc
public func fetchExisting<T: DynamicObject>(_ objectID: NSManagedObjectID) -> T? {
public func fetchExisting<D: DynamicObject>(_ objectID: NSManagedObjectID) -> D? {
do {
let existingObject = try self.existingObject(with: objectID)
return T.cs_fromRaw(object: existingObject)
return D.cs_fromRaw(object: existingObject)
}
catch _ {
@@ -89,25 +89,25 @@ extension NSManagedObjectContext: FetchableSource, QueryableSource {
}
@nonobjc
public func fetchExisting<T: DynamicObject, S: Sequence>(_ objects: S) -> [T] where S.Iterator.Element == T {
public func fetchExisting<D: DynamicObject, S: Sequence>(_ objects: S) -> [D] where S.Iterator.Element == D {
return objects.flatMap({ self.fetchExisting($0.cs_id()) })
}
@nonobjc
public func fetchExisting<T: DynamicObject, S: Sequence>(_ objectIDs: S) -> [T] where S.Iterator.Element == NSManagedObjectID {
public func fetchExisting<D: DynamicObject, S: Sequence>(_ objectIDs: S) -> [D] where S.Iterator.Element == NSManagedObjectID {
return objectIDs.flatMap({ self.fetchExisting($0) })
}
@nonobjc
public func fetchOne<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> T? {
public func fetchOne<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> D? {
return self.fetchOne(from, fetchClauses)
}
@nonobjc
public func fetchOne<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
public func fetchOne<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> D? {
let fetchRequest = CoreStoreFetchRequest()
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
@@ -123,14 +123,21 @@ extension NSManagedObjectContext: FetchableSource, QueryableSource {
return self.fetchOne(fetchRequest.dynamicCast()).flatMap(from.entityClass.cs_fromRaw)
}
// TODO: docs
@nonobjc
public func fetchAll<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [T]? {
public func fetchOne<B: FetchChainableBuilderType>(_ clauseChain: B) -> B.ObjectType? {
return self.fetchOne(clauseChain.from, clauseChain.fetchClauses)
}
@nonobjc
public func fetchAll<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> [D]? {
return self.fetchAll(from, fetchClauses)
}
@nonobjc
public func fetchAll<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [T]? {
public func fetchAll<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> [D]? {
let fetchRequest = CoreStoreFetchRequest()
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
@@ -147,14 +154,21 @@ extension NSManagedObjectContext: FetchableSource, QueryableSource {
return self.fetchAll(fetchRequest.dynamicCast())?.map(entityClass.cs_fromRaw)
}
// TODO: docs
@nonobjc
public func fetchCount<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> Int? {
public func fetchAll<B: FetchChainableBuilderType>(_ clauseChain: B) -> [B.ObjectType]? {
return self.fetchAll(clauseChain.from, clauseChain.fetchClauses)
}
@nonobjc
public func fetchCount<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> Int? {
return self.fetchCount(from, fetchClauses)
}
@nonobjc
public func fetchCount<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> Int? {
public func fetchCount<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> Int? {
let fetchRequest = CoreStoreFetchRequest()
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
@@ -167,14 +181,21 @@ extension NSManagedObjectContext: FetchableSource, QueryableSource {
return self.fetchCount(fetchRequest.dynamicCast())
}
// TODO: docs
@nonobjc
public func fetchObjectID<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
public func fetchCount<B: FetchChainableBuilderType>(_ clauseChain: B) -> Int? {
return self.fetchCount(clauseChain.from, clauseChain.fetchClauses)
}
@nonobjc
public func fetchObjectID<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
return self.fetchObjectID(from, fetchClauses)
}
@nonobjc
public func fetchObjectID<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
public func fetchObjectID<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
let fetchRequest = CoreStoreFetchRequest()
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
@@ -190,14 +211,21 @@ extension NSManagedObjectContext: FetchableSource, QueryableSource {
return self.fetchObjectID(fetchRequest.dynamicCast())
}
// TODO: docs
@nonobjc
public func fetchObjectIDs<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
public func fetchObjectID<B: FetchChainableBuilderType>(_ clauseChain: B) -> NSManagedObjectID? {
return self.fetchObjectID(clauseChain.from, clauseChain.fetchClauses)
}
@nonobjc
public func fetchObjectIDs<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
return self.fetchObjectIDs(from, fetchClauses)
}
@nonobjc
public func fetchObjectIDs<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
public func fetchObjectIDs<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
let fetchRequest = CoreStoreFetchRequest()
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
@@ -213,6 +241,13 @@ extension NSManagedObjectContext: FetchableSource, QueryableSource {
return self.fetchObjectIDs(fetchRequest.dynamicCast())
}
// TODO: docs
@nonobjc
public func fetchObjectIDs<B: FetchChainableBuilderType>(_ clauseChain: B) -> [NSManagedObjectID]? {
return self.fetchObjectIDs(clauseChain.from, clauseChain.fetchClauses)
}
@nonobjc
internal func fetchObjectIDs(_ fetchRequest: NSFetchRequest<NSManagedObjectID>) -> [NSManagedObjectID]? {
@@ -244,13 +279,13 @@ extension NSManagedObjectContext: FetchableSource, QueryableSource {
// MARK: QueryableSource
@nonobjc
public func queryValue<T, U: QueryableAttributeType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
public func queryValue<D, U: QueryableAttributeType>(_ from: From<D>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
return self.queryValue(from, selectClause, queryClauses)
}
@nonobjc
public func queryValue<T, U: QueryableAttributeType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
public func queryValue<D, U: QueryableAttributeType>(_ from: From<D>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
let fetchRequest = CoreStoreFetchRequest()
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
@@ -269,13 +304,13 @@ extension NSManagedObjectContext: FetchableSource, QueryableSource {
}
@nonobjc
public func queryAttributes<T>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]? {
public func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]? {
return self.queryAttributes(from, selectClause, queryClauses)
}
@nonobjc
public func queryAttributes<T>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]? {
public func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]? {
let fetchRequest = CoreStoreFetchRequest()
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
@@ -305,13 +340,7 @@ extension NSManagedObjectContext: FetchableSource, QueryableSource {
// MARK: Deleting
@nonobjc
internal func deleteAll<T>(_ from: From<T>, _ deleteClauses: DeleteClause...) -> Int? {
return self.deleteAll(from, deleteClauses)
}
@nonobjc
internal func deleteAll<T>(_ from: From<T>, _ deleteClauses: [DeleteClause]) -> Int? {
internal func deleteAll<D>(_ from: From<D>, _ deleteClauses: [FetchClause]) -> Int? {
let fetchRequest = CoreStoreFetchRequest()
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
@@ -347,9 +376,9 @@ internal extension NSManagedObjectContext {
// MARK: Fetching
@nonobjc
internal func fetchOne<T: NSManagedObject>(_ fetchRequest: NSFetchRequest<T>) -> T? {
internal func fetchOne<D: NSManagedObject>(_ fetchRequest: NSFetchRequest<D>) -> D? {
var fetchResults: [T]?
var fetchResults: [D]?
var fetchError: Error?
self.performAndWait {
@@ -374,9 +403,9 @@ internal extension NSManagedObjectContext {
}
@nonobjc
internal func fetchAll<T: NSManagedObject>(_ fetchRequest: NSFetchRequest<T>) -> [T]? {
internal func fetchAll<D: NSManagedObject>(_ fetchRequest: NSFetchRequest<D>) -> [D]? {
var fetchResults: [T]?
var fetchResults: [D]?
var fetchError: Error?
self.performAndWait {
@@ -555,7 +584,7 @@ internal extension NSManagedObjectContext {
// MARK: Deleting
@nonobjc
internal func deleteAll<T: NSManagedObject>(_ fetchRequest: NSFetchRequest<T>) -> Int? {
internal func deleteAll<D: NSManagedObject>(_ fetchRequest: NSFetchRequest<D>) -> Int? {
var numberOfDeletedObjects: Int?
var fetchError: Error?

View File

@@ -273,7 +273,7 @@ public final class ObjectMonitor<D: DynamicObject>: Equatable {
context: context,
fetchRequest: fetchRequest.dynamicCast(),
from: From<ObjectType>([objectID.persistentStore?.configurationName]),
applyFetchClauses: Where("SELF", isEqualTo: objectID).applyToFetchRequest
applyFetchClauses: Where<ObjectType>("SELF", isEqualTo: objectID).applyToFetchRequest
)
let fetchedResultsControllerDelegate = FetchedResultsControllerDelegate()

View File

@@ -32,31 +32,12 @@ import CoreData
public typealias KeyPathString = String
// MARK: - SortKey
/**
The `SortKey` is passed to the `OrderBy` clause to indicate the sort keys and their sort direction.
*/
public enum SortKey {
/**
Indicates that the `KeyPathString` should be sorted in ascending order
*/
case ascending(KeyPathString)
/**
Indicates that the `KeyPathString` should be sorted in descending order
*/
case descending(KeyPathString)
}
// MARK: - OrderBy
/**
The `OrderBy` clause specifies the sort order for results for a fetch or a query.
*/
public struct OrderBy: FetchClause, QueryClause, DeleteClause, Hashable {
public struct OrderBy<D: DynamicObject>: OrderByClause, FetchClause, QueryClause, DeleteClause, Hashable {
/**
Combines two `OrderBy` sort descriptors together
@@ -74,11 +55,6 @@ public struct OrderBy: FetchClause, QueryClause, DeleteClause, Hashable {
left = left + right
}
/**
The list of sort descriptors
*/
public let sortDescriptors: [NSSortDescriptor]
/**
Initializes a `OrderBy` clause with an empty list of sort descriptors
*/
@@ -112,21 +88,9 @@ public struct OrderBy: FetchClause, QueryClause, DeleteClause, Hashable {
- parameter sortKey: a series of `SortKey`s
*/
public init(_ sortKey: [SortKey]) {
public init(_ sortKeys: [SortKey]) {
self.init(
sortKey.map { sortKey -> NSSortDescriptor in
switch sortKey {
case .ascending(let keyPath):
return NSSortDescriptor(key: keyPath, ascending: true)
case .descending(let keyPath):
return NSSortDescriptor(key: keyPath, ascending: false)
}
}
)
self.init(sortKeys.map({ $0.descriptor }))
}
/**
@@ -141,6 +105,13 @@ public struct OrderBy: FetchClause, QueryClause, DeleteClause, Hashable {
}
// MARK: OrderByClause
public typealias ObjectType = D
public let sortDescriptors: [NSSortDescriptor]
// MARK: FetchClause, QueryClause, DeleteClause
public func applyToFetchRequest<ResultType>(_ fetchRequest: NSFetchRequest<ResultType>) {
@@ -171,17 +142,154 @@ public struct OrderBy: FetchClause, QueryClause, DeleteClause, Hashable {
return (self.sortDescriptors as NSArray).hashValue
}
// MARK: - SortKey
/**
The `SortKey` is passed to the `OrderBy` clause to indicate the sort keys and their sort direction.
*/
public struct SortKey {
// MARK: Raw Key Paths
/**
Indicates that the `KeyPathString` should be sorted in ascending order
*/
public static func ascending(_ keyPath: KeyPathString) -> SortKey {
return SortKey(descriptor: .init(key: keyPath, ascending: true))
}
/**
Indicates that the `KeyPathString` should be sorted in descending order
*/
public static func descending(_ keyPath: KeyPathString) -> SortKey {
return SortKey(descriptor: .init(key: keyPath, ascending: false))
}
// MARK: NSManagedObject Key Paths
/**
Indicates that the `KeyPathString` should be sorted in ascending order
*/
public static func ascending<T>(_ keyPath: KeyPath<D, T>) -> SortKey where D: NSManagedObject {
return .ascending(keyPath._kvcKeyPathString!)
}
/**
Indicates that the `KeyPathString` should be sorted in descending order
*/
public static func descending<T>(_ keyPath: KeyPath<D, T>) -> SortKey where D: NSManagedObject {
return .descending(keyPath._kvcKeyPathString!)
}
// MARK: CoreStoreObject Key Paths
/**
Indicates that the `KeyPathString` should be sorted in ascending order
*/
public static func ascending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: ValueContainer<D>.Required<T> {
return .ascending(D.meta[keyPath: attribute].keyPath)
}
/**
Indicates that the `KeyPathString` should be sorted in ascending order
*/
public static func ascending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: ValueContainer<D>.Optional<T> {
return .ascending(D.meta[keyPath: attribute].keyPath)
}
/**
Indicates that the `KeyPathString` should be sorted in ascending order
*/
public static func ascending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: TransformableContainer<D>.Required<T> {
return .ascending(D.meta[keyPath: attribute].keyPath)
}
/**
Indicates that the `KeyPathString` should be sorted in ascending order
*/
public static func ascending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: TransformableContainer<D>.Optional<T> {
return .ascending(D.meta[keyPath: attribute].keyPath)
}
/**
Indicates that the `KeyPathString` should be sorted in descending order
*/
public static func descending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: ValueContainer<D>.Required<T> {
return .descending(D.meta[keyPath: attribute].keyPath)
}
/**
Indicates that the `KeyPathString` should be sorted in descending order
*/
public static func descending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: ValueContainer<D>.Optional<T> {
return .descending(D.meta[keyPath: attribute].keyPath)
}
/**
Indicates that the `KeyPathString` should be sorted in descending order
*/
public static func descending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: TransformableContainer<D>.Required<T> {
return .descending(D.meta[keyPath: attribute].keyPath)
}
/**
Indicates that the `KeyPathString` should be sorted in descending order
*/
public static func descending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: TransformableContainer<D>.Optional<T> {
return .descending(D.meta[keyPath: attribute].keyPath)
}
// MARK: Private
fileprivate let descriptor: NSSortDescriptor
}
}
// MARK: - Sequence where Element == OrderBy
// MARK: - OrderByClause
public extension Sequence where Iterator.Element == OrderBy {
/**
Abstracts the `OrderBy` clause for protocol utilities.
*/
public protocol OrderByClause {
/**
The `DynamicObject` type associated with the clause
*/
associatedtype ObjectType: DynamicObject
/**
The `NSSortDescriptor` array for the fetch or query
*/
var sortDescriptors: [NSSortDescriptor] { get }
}
// MARK: - Sequence where Iterator.Element: OrderByClause
public extension Sequence where Iterator.Element: OrderByClause {
/**
Combines multiple `OrderBy` predicates together
*/
public func combined() -> OrderBy {
public func combined() -> OrderBy<Iterator.Element.ObjectType> {
return OrderBy(self.flatMap({ $0.sortDescriptors }))
}

View File

@@ -44,7 +44,7 @@ public protocol QueryableSource: class {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
func queryValue<T, U: QueryableAttributeType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U?
func queryValue<D, U: QueryableAttributeType>(_ from: From<D>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U?
/**
Queries aggregate values as specified by the `QueryClause`s. Requires at least a `Select` clause, and optional `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
@@ -56,7 +56,7 @@ public protocol QueryableSource: class {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
func queryValue<T, U: QueryableAttributeType>(_ from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U?
func queryValue<D, U: QueryableAttributeType>(_ from: From<D>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U?
/**
Queries a dictionary of attribute values as specified by the `QueryClause`s. Requires at least a `Select` clause, and optional `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
@@ -68,7 +68,7 @@ public protocol QueryableSource: class {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
func queryAttributes<T>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]?
func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[String: Any]]?
/**
Queries a dictionary of attribute values as specified by the `QueryClause`s. Requires at least a `Select` clause, and optional `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
@@ -80,7 +80,7 @@ public protocol QueryableSource: class {
- parameter queryClauses: a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.
- returns: the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.
*/
func queryAttributes<T>(_ from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]?
func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[String: Any]]?
/**
The internal `NSManagedObjectContext` managed by this `QueryableSource`. Using this context directly should typically be avoided, and is provided by CoreStore only for extremely specialized cases.

View File

@@ -55,7 +55,7 @@ public final class SynchronousDataTransaction: BaseDataTransaction {
- parameter into: the `Into` clause indicating the destination `NSManagedObject` or `CoreStoreObject` entity type and the destination configuration
- returns: a new `NSManagedObject` or `CoreStoreObject` instance of the specified entity type.
*/
public override func create<T>(_ into: Into<T>) -> T {
public override func create<D>(_ into: Into<D>) -> D {
CoreStore.assert(
!self.isCommitted,
@@ -71,7 +71,7 @@ public final class SynchronousDataTransaction: BaseDataTransaction {
- parameter object: the `NSManagedObject` or `CoreStoreObject` to be edited
- returns: an editable proxy for the specified `NSManagedObject` or `CoreStoreObject`.
*/
public override func edit<T: DynamicObject>(_ object: T?) -> T? {
public override func edit<D: DynamicObject>(_ object: D?) -> D? {
CoreStore.assert(
!self.isCommitted,
@@ -88,7 +88,7 @@ public final class SynchronousDataTransaction: BaseDataTransaction {
- parameter objectID: the `NSManagedObjectID` for the object to be edited
- returns: an editable proxy for the specified `NSManagedObject` or `CoreStoreObject`.
*/
public override func edit<T>(_ into: Into<T>, _ objectID: NSManagedObjectID) -> T? {
public override func edit<D>(_ into: Into<D>, _ objectID: NSManagedObjectID) -> D? {
CoreStore.assert(
!self.isCommitted,
@@ -103,7 +103,7 @@ public final class SynchronousDataTransaction: BaseDataTransaction {
- parameter object: the `NSManagedObject` or `CoreStoreObject` type to be deleted
*/
public override func delete<T: DynamicObject>(_ object: T?) {
public override func delete<D: DynamicObject>(_ object: D?) {
CoreStore.assert(
!self.isCommitted,
@@ -120,7 +120,7 @@ public final class SynchronousDataTransaction: BaseDataTransaction {
- parameter object2: another `DynamicObject` to be deleted
- parameter objects: other `DynamicObject`s to be deleted
*/
public override func delete<T: DynamicObject>(_ object1: T?, _ object2: T?, _ objects: T?...) {
public override func delete<D: DynamicObject>(_ object1: D?, _ object2: D?, _ objects: D?...) {
CoreStore.assert(
!self.isCommitted,

View File

@@ -38,7 +38,7 @@ public extension UnsafeDataTransaction {
- parameter object: the `DynamicObject` to observe changes from
- returns: a `ObjectMonitor` that monitors changes to `object`
*/
public func monitorObject<T>(_ object: T) -> ObjectMonitor<T> {
public func monitorObject<D>(_ object: D) -> ObjectMonitor<D> {
return ObjectMonitor(
unsafeTransaction: self,
@@ -53,7 +53,7 @@ public extension UnsafeDataTransaction {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list
*/
public func monitorList<T>(_ from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
public func monitorList<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> ListMonitor<D> {
return self.monitorList(from, fetchClauses)
}
@@ -65,10 +65,10 @@ public extension UnsafeDataTransaction {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list
*/
public func monitorList<T>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
public func monitorList<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> ListMonitor<D> {
CoreStore.assert(
fetchClauses.filter { $0 is OrderBy }.count > 0,
fetchClauses.filter { $0 is OrderBy<D> }.count > 0,
"A ListMonitor requires an OrderBy clause."
)
@@ -90,7 +90,7 @@ public extension UnsafeDataTransaction {
- parameter from: a `From` clause indicating the entity type
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/
public func monitorList<T>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) {
public func monitorList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ fetchClauses: FetchClause...) {
self.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses)
}
@@ -102,10 +102,10 @@ public extension UnsafeDataTransaction {
- parameter from: a `From` clause indicating the entity type
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/
public func monitorList<T>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) {
public func monitorList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ fetchClauses: [FetchClause]) {
CoreStore.assert(
fetchClauses.filter { $0 is OrderBy }.count > 0,
fetchClauses.filter { $0 is OrderBy<D> }.count > 0,
"A ListMonitor requires an OrderBy clause."
)
@@ -129,7 +129,7 @@ public extension UnsafeDataTransaction {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list
*/
public func monitorSectionedList<T>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
public func monitorSectionedList<D>(_ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<D> {
return self.monitorSectionedList(from, sectionBy, fetchClauses)
}
@@ -142,10 +142,10 @@ public extension UnsafeDataTransaction {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list
*/
public func monitorSectionedList<T>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
public func monitorSectionedList<D>(_ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<D> {
CoreStore.assert(
fetchClauses.filter { $0 is OrderBy }.count > 0,
fetchClauses.filter { $0 is OrderBy<D> }.count > 0,
"A ListMonitor requires an OrderBy clause."
)
@@ -168,7 +168,7 @@ public extension UnsafeDataTransaction {
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/
public func monitorSectionedList<T>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
public func monitorSectionedList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
self.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses)
}
@@ -181,10 +181,10 @@ public extension UnsafeDataTransaction {
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/
public func monitorSectionedList<T>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
public func monitorSectionedList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
CoreStore.assert(
fetchClauses.filter { $0 is OrderBy }.count > 0,
fetchClauses.filter { $0 is OrderBy<D> }.count > 0,
"A ListMonitor requires an OrderBy clause."
)

View File

@@ -0,0 +1,220 @@
//
// Where+NSManagedObject.swift
// CoreStore
//
// Copyright © 2017 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import CoreData
import Foundation
// MARK: - KeyPath where Root: NSManagedObject, Value: QueryableAttributeType & Equatable
public extension KeyPath where Root: NSManagedObject, Value: QueryableAttributeType & Equatable {
public static func == (_ keyPath: KeyPath<Root, Value>, _ value: Value) -> Where<Root> {
return Where(keyPath._kvcKeyPathString!, isEqualTo: value)
}
public static func != (_ keyPath: KeyPath<Root, Value>, _ value: Value) -> Where<Root> {
return !Where(keyPath._kvcKeyPathString!, isEqualTo: value)
}
public static func ~= <S: Sequence>(_ sequence: S, _ keyPath: KeyPath<Root, Value>) -> Where<Root> where S.Iterator.Element == Value {
return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence)
}
}
// MARK: - KeyPath where Root: NSManagedObject, Value: Optional<QueryableAttributeType & Equatable>
public extension KeyPath where Root: NSManagedObject {
public static func == <V: QueryableAttributeType & Equatable> (_ keyPath: KeyPath<Root, Value>, _ value: Value) -> Where<Root> where Value == Optional<V> {
return Where(keyPath._kvcKeyPathString!, isEqualTo: value)
}
public static func != <V: QueryableAttributeType & Equatable> (_ keyPath: KeyPath<Root, Value>, _ value: Value) -> Where<Root> where Value == Optional<V> {
return !Where(keyPath._kvcKeyPathString!, isEqualTo: value)
}
public static func ~= <S: Sequence, V: QueryableAttributeType & Equatable>(_ sequence: S, _ keyPath: KeyPath<Root, Value>) -> Where<Root> where Value == Optional<V>, S.Iterator.Element == V {
return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence)
}
}
// MARK: - KeyPath where Root: NSManagedObject, Value: QueryableAttributeType
public extension KeyPath where Root: NSManagedObject, Value: QueryableAttributeType {
public static func < (_ keyPath: KeyPath<Root, Value>, _ value: Value) -> Where<Root> {
return Where("%K < %@", keyPath._kvcKeyPathString!, value)
}
public static func > (_ keyPath: KeyPath<Root, Value>, _ value: Value) -> Where<Root> {
return Where("%K > %@", keyPath._kvcKeyPathString!, value)
}
public static func <= (_ keyPath: KeyPath<Root, Value>, _ value: Value) -> Where<Root> {
return Where("%K <= %@", keyPath._kvcKeyPathString!, value)
}
public static func >= (_ keyPath: KeyPath<Root, Value>, _ value: Value) -> Where<Root> {
return Where("%K >= %@", keyPath._kvcKeyPathString!, value)
}
}
// MARK: - KeyPath where Root: NSManagedObject, Value: Optional<QueryableAttributeType>
public extension KeyPath where Root: NSManagedObject {
public static func < <V: QueryableAttributeType> (_ keyPath: KeyPath<Root, Value>, _ value: Value) -> Where<Root> where Value == Optional<V> {
if let value = value {
return Where("%K < %@", keyPath._kvcKeyPathString!, value)
}
else {
return Where("%K < nil", keyPath._kvcKeyPathString!)
}
}
public static func > <V: QueryableAttributeType> (_ keyPath: KeyPath<Root, Value>, _ value: Value) -> Where<Root> where Value == Optional<V> {
if let value = value {
return Where("%K > %@", keyPath._kvcKeyPathString!, value)
}
else {
return Where("%K > nil", keyPath._kvcKeyPathString!)
}
}
public static func <= <V: QueryableAttributeType> (_ keyPath: KeyPath<Root, Value>, _ value: Value) -> Where<Root> where Value == Optional<V> {
if let value = value {
return Where("%K <= %@", keyPath._kvcKeyPathString!, value)
}
else {
return Where("%K <= nil", keyPath._kvcKeyPathString!)
}
}
public static func >= <V: QueryableAttributeType> (_ keyPath: KeyPath<Root, Value>, _ value: Value) -> Where<Root> where Value == Optional<V> {
if let value = value {
return Where("%K >= %@", keyPath._kvcKeyPathString!, value)
}
else {
return Where("%K >= nil", keyPath._kvcKeyPathString!)
}
}
}
// MARK: - KeyPath where Root: NSManagedObject, Value: NSManagedObject
public extension KeyPath where Root: NSManagedObject, Value: NSManagedObject {
public static func == (_ keyPath: KeyPath<Root, Value>, _ object: Value) -> Where<Root> {
return Where(keyPath._kvcKeyPathString!, isEqualTo: object)
}
public static func != (_ keyPath: KeyPath<Root, Value>, _ object: Value) -> Where<Root> {
return !Where(keyPath._kvcKeyPathString!, isEqualTo: object)
}
public static func ~= <S: Sequence>(_ sequence: S, _ keyPath: KeyPath<Root, Value>) -> Where<Root> where S.Iterator.Element == Value {
return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence)
}
public static func == (_ keyPath: KeyPath<Root, Value>, _ objectID: NSManagedObjectID?) -> Where<Root> {
return Where(keyPath._kvcKeyPathString!, isEqualTo: objectID)
}
public static func != (_ keyPath: KeyPath<Root, Value>, _ objectID: NSManagedObjectID?) -> Where<Root> {
return !Where(keyPath._kvcKeyPathString!, isEqualTo: objectID)
}
public static func ~= <S: Sequence>(_ sequence: S, _ keyPath: KeyPath<Root, Value>) -> Where<Root> where S.Iterator.Element == NSManagedObjectID {
return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence)
}
}
// MARK: - KeyPath where Root: NSManagedObject, Value: Optional<NSManagedObject>
public extension KeyPath where Root: NSManagedObject {
public static func == <V: NSManagedObject> (_ keyPath: KeyPath<Root, Value>, _ object: Value) -> Where<Root> where Value == Optional<V> {
return Where(keyPath._kvcKeyPathString!, isEqualTo: object)
}
public static func != <V: NSManagedObject> (_ keyPath: KeyPath<Root, Value>, _ object: Value) -> Where<Root> where Value == Optional<V> {
return !Where(keyPath._kvcKeyPathString!, isEqualTo: object)
}
public static func ~= <S: Sequence, V: NSManagedObject>(_ sequence: S, _ keyPath: KeyPath<Root, Value>) -> Where<Root> where Value == Optional<V>, S.Iterator.Element == V {
return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence)
}
public static func == <V: NSManagedObject> (_ keyPath: KeyPath<Root, Value>, _ objectID: NSManagedObjectID?) -> Where<Root> where Value == Optional<V> {
return Where(keyPath._kvcKeyPathString!, isEqualTo: objectID)
}
public static func != <V: NSManagedObject> (_ keyPath: KeyPath<Root, Value>, _ objectID: NSManagedObjectID?) -> Where<Root> where Value == Optional<V> {
return !Where(keyPath._kvcKeyPathString!, isEqualTo: objectID)
}
public static func ~= <S: Sequence, V: NSManagedObject>(_ sequence: S, _ keyPath: KeyPath<Root, Value>) -> Where<Root> where Value == Optional<V>, S.Iterator.Element == NSManagedObjectID {
return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence)
}
}

View File

@@ -32,7 +32,7 @@ import CoreData
/**
The `Where` clause specifies the conditions for a fetch or a query.
*/
public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
public struct Where<D: DynamicObject>: WhereClause, FetchClause, QueryClause, DeleteClause, Hashable {
/**
Combines two `Where` predicates together using `AND` operator
@@ -126,11 +126,6 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
return Where(NSCompoundPredicate(type: .not, subpredicates: [clause.predicate]))
}
/**
The `NSPredicate` for the fetch or query
*/
public let predicate: NSPredicate
/**
Initializes a `Where` clause with a predicate that always evaluates to `true`
*/
@@ -188,7 +183,7 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
- parameter keyPath: the keyPath to compare with
- parameter value: the arguments for the `==` operator
*/
public init<T: QueryableAttributeType>(_ keyPath: KeyPathString, isEqualTo value: T?) {
public init<U: QueryableAttributeType>(_ keyPath: KeyPathString, isEqualTo value: U?) {
switch value {
@@ -207,12 +202,11 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
- parameter keyPath: the keyPath to compare with
- parameter object: the arguments for the `==` operator
*/
public init<T: DynamicObject>(_ keyPath: KeyPathString, isEqualTo object: T?) {
public init<D: DynamicObject>(_ keyPath: KeyPathString, isEqualTo object: D?) {
switch object {
case nil,
is NSNull:
case nil:
self.init(NSPredicate(format: "\(keyPath) == nil"))
case let object?:
@@ -220,6 +214,17 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
}
}
/**
Initializes a `Where` clause that compares equality
- parameter keyPath: the keyPath to compare with
- parameter objectID: the arguments for the `==` operator
*/
public init(_ keyPath: KeyPathString, isEqualTo objectID: NSManagedObjectID) {
self.init(NSPredicate(format: "\(keyPath) == %@", argumentArray: [objectID]))
}
/**
Initializes a `Where` clause that compares membership
@@ -242,6 +247,17 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
self.init(NSPredicate(format: "\(keyPath) IN %@", list.map({ $0.cs_id() }) as NSArray))
}
/**
Initializes a `Where` clause that compares membership
- parameter keyPath: the keyPath to compare with
- parameter list: the sequence to check membership of
*/
public init<S: Sequence>(_ keyPath: KeyPathString, isMemberOf list: S) where S.Iterator.Element: NSManagedObjectID {
self.init(NSPredicate(format: "\(keyPath) IN %@", list.map({ $0 }) as NSArray))
}
/**
Initializes a `Where` clause with an `NSPredicate`
@@ -253,6 +269,13 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
}
// MARK: WhereClause
public typealias ObjectType = D
public let predicate: NSPredicate
// MARK: FetchClause, QueryClause, DeleteClause
public func applyToFetchRequest<ResultType>(_ fetchRequest: NSFetchRequest<ResultType>) {
@@ -286,14 +309,33 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
}
// MARK: - Sequence where Element == Where
// MARK: - WhereClause
public extension Sequence where Iterator.Element == Where {
/**
Abstracts the `Where` clause for protocol utilities.
*/
public protocol WhereClause {
/**
The `DynamicObject` type associated with the clause
*/
associatedtype ObjectType: DynamicObject
/**
The `NSPredicate` for the fetch or query
*/
var predicate: NSPredicate { get }
}
// MARK: - Sequence where Iterator.Element: WhereClause
public extension Sequence where Iterator.Element: WhereClause {
/**
Combines multiple `Where` predicates together using `AND` operator
*/
public func combinedByAnd() -> Where {
public func combinedByAnd() -> Where<Iterator.Element.ObjectType> {
return Where(NSCompoundPredicate(type: .and, subpredicates: self.map({ $0.predicate })))
}
@@ -301,7 +343,7 @@ public extension Sequence where Iterator.Element == Where {
/**
Combines multiple `Where` predicates together using `OR` operator
*/
public func combinedByOr() -> Where {
public func combinedByOr() -> Where<Iterator.Element.ObjectType> {
return Where(NSCompoundPredicate(type: .or, subpredicates: self.map({ $0.predicate })))
}