diff --git a/Cartfile b/Cartfile index 3aee48e..4f4c0f2 100644 --- a/Cartfile +++ b/Cartfile @@ -1 +1 @@ -github "JohnEstropia/GCDKit" == 1.2.4 +github "JohnEstropia/GCDKit" == 1.2.5 diff --git a/Sources/Convenience/NSFetchedResultsController+Convenience.swift b/Sources/Convenience/NSFetchedResultsController+Convenience.swift index e9f3310..41882df 100644 --- a/Sources/Convenience/NSFetchedResultsController+Convenience.swift +++ b/Sources/Convenience/NSFetchedResultsController+Convenience.swift @@ -35,23 +35,162 @@ public extension NSFetchedResultsController { Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction. - parameter dataStack: the `DataStack` to observe objects from - - parameter fetchRequest: the `NSFetchRequest` instance to use with the `NSFetchedResultsController` - - parameter from: an optional `From` clause indicating the entity type. If not specified, the `fetchRequest` argument's `entity` property should already be set. - - parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections. + - parameter from: a `From` clause indicating the entity type + - 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. + - returns: an `NSFetchedResultsController` that observes a `DataStack` + */ + @nonobjc + public static func createFor(dataStack: DataStack, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> NSFetchedResultsController { + + return self.createFromContext( + dataStack.mainContext, + fetchRequest: CoreStoreFetchRequest(), + from: from, + sectionBy: sectionBy, + fetchClauses: fetchClauses + ) + } + + /** + Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction. + + - parameter dataStack: the `DataStack` to observe objects from + - parameter from: a `From` clause indicating the entity type + - 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. + - returns: an `NSFetchedResultsController` that observes a `DataStack` + */ + @nonobjc + public static func createFor(dataStack: DataStack, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController { + + return self.createFromContext( + dataStack.mainContext, + fetchRequest: CoreStoreFetchRequest(), + from: from, + sectionBy: sectionBy, + fetchClauses: fetchClauses + ) + } + + /** + Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction. + + - parameter dataStack: the `DataStack` to observe objects from + - 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. + - returns: an `NSFetchedResultsController` that observes a `DataStack` + */ + @nonobjc + public static func createFor(dataStack: DataStack, _ from: From, _ fetchClauses: FetchClause...) -> NSFetchedResultsController { + + return self.createFromContext( + dataStack.mainContext, + fetchRequest: CoreStoreFetchRequest(), + from: from, + sectionBy: nil, + fetchClauses: fetchClauses + ) + } + + /** + Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction. + + - parameter dataStack: the `DataStack` to observe objects from + - 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. + - returns: an `NSFetchedResultsController` that observes a `DataStack` + */ + @nonobjc + public static func createFor(dataStack: DataStack, _ from: From, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController { + + return self.createFromContext( + dataStack.mainContext, + fetchRequest: CoreStoreFetchRequest(), + from: from, + sectionBy: nil, + fetchClauses: fetchClauses + ) + } + + /** + Utility for creating an `NSFetchedResultsController` from an `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction. + + - parameter transaction: the `UnsafeDataTransaction` to observe objects from + - parameter from: a `From` clause indicating the entity type + - 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. + - returns: an `NSFetchedResultsController` that observes an `UnsafeDataTransaction` + */ + @nonobjc + public static func createFor(transaction: UnsafeDataTransaction, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> NSFetchedResultsController { + + return self.createFromContext( + transaction.context, + fetchRequest: CoreStoreFetchRequest(), + from: from, + sectionBy: sectionBy, + fetchClauses: fetchClauses + ) + } + + /** + Utility for creating an `NSFetchedResultsController` from an `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction. + + - parameter transaction: the `UnsafeDataTransaction` to observe objects from + - parameter from: a `From` clause indicating the entity type + - 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. + - returns: an `NSFetchedResultsController` that observes an `UnsafeDataTransaction` + */ + @nonobjc + public static func createFor(transaction: UnsafeDataTransaction, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController { + + return self.createFromContext( + transaction.context, + fetchRequest: CoreStoreFetchRequest(), + from: from, + sectionBy: sectionBy, + fetchClauses: fetchClauses + ) + } + + /** + Utility for creating an `NSFetchedResultsController` from an `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction. + + - parameter transaction: the `UnsafeDataTransaction` to observe objects from + - 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. + - returns: an `NSFetchedResultsController` that observes an `UnsafeDataTransaction` + */ + @nonobjc + public static func createFor(transaction: UnsafeDataTransaction, _ from: From, _ fetchClauses: FetchClause...) -> NSFetchedResultsController { + + return self.createFromContext( + transaction.context, + fetchRequest: CoreStoreFetchRequest(), + from: from, + sectionBy: nil, + fetchClauses: fetchClauses + ) + } + + /** + Utility for creating an `NSFetchedResultsController` from an `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction. + + - parameter transaction: the `UnsafeDataTransaction` to observe objects from + - 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. */ @nonobjc - public static func createForStack(dataStack: DataStack, fetchRequest: NSFetchRequest, from: From? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) -> NSFetchedResultsController { + public static func createFor(transaction: UnsafeDataTransaction, _ from: From, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController { - return CoreStoreFetchedResultsController( - context: dataStack.mainContext, - fetchRequest: fetchRequest, + return self.createFromContext( + transaction.context, + fetchRequest: CoreStoreFetchRequest(), from: from, - sectionBy: sectionBy, - applyFetchClauses: { fetchRequest in - - fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) } - } + sectionBy: nil, + fetchClauses: fetchClauses ) } diff --git a/Sources/Internal/CoreStoreFetchedResultsController.swift b/Sources/Internal/CoreStoreFetchedResultsController.swift index bc35a1d..3c01070 100644 --- a/Sources/Internal/CoreStoreFetchedResultsController.swift +++ b/Sources/Internal/CoreStoreFetchedResultsController.swift @@ -49,6 +49,10 @@ internal final class CoreStoreFetchedResultsController: NSFetchedResultsControll @nonobjc internal init(context: NSManagedObjectContext, fetchRequest: NSFetchRequest, from: From? = nil, sectionBy: SectionBy? = nil, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void) { + CoreStore.assert( + fetchClauses.filter { $0 is OrderBy }.count > 0, + "An \(typeName(NSFetchedResultsController)) requires an OrderBy clause." + ) from?.applyToFetchRequest(fetchRequest, context: context, applyAffectedStores: false) applyFetchClauses(fetchRequest: fetchRequest)