From f617f2cc5312ba1ce333360d0677c46cef9c340b Mon Sep 17 00:00:00 2001 From: John Rommel Estropia Date: Sat, 28 May 2016 01:47:48 +0900 Subject: [PATCH] Simpler API for creating CoreStore-managed NSFetchedResultsControllers, useful for external SDK's that require them --- CoreStore.podspec | 2 +- ...FetchedResultsController+Convenience.swift | 167 +++++++++++++++++- CoreStore/Info.plist | 2 +- .../CoreStoreFetchedResultsController.swift | 4 + 4 files changed, 171 insertions(+), 4 deletions(-) diff --git a/CoreStore.podspec b/CoreStore.podspec index 00b218a..cea7eb3 100644 --- a/CoreStore.podspec +++ b/CoreStore.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "CoreStore" - s.version = "1.6.7" + s.version = "1.6.8" s.license = "MIT" s.summary = "Unleashing the real power of Core Data with the elegance and safety of Swift" s.homepage = "https://github.com/JohnEstropia/CoreStore" diff --git a/CoreStore/Convenience Helpers/NSFetchedResultsController+Convenience.swift b/CoreStore/Convenience Helpers/NSFetchedResultsController+Convenience.swift index 6dd1452..351fe00 100644 --- a/CoreStore/Convenience Helpers/NSFetchedResultsController+Convenience.swift +++ b/CoreStore/Convenience Helpers/NSFetchedResultsController+Convenience.swift @@ -32,8 +32,170 @@ import CoreData public extension NSFetchedResultsController { /** - Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful to partially support Objective-C classes by passing an `NSFetchedResultsController` instance instead of a `ListMonitor`. + 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 CoreStoreFetchedResultsController( + context: 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 CoreStoreFetchedResultsController( + context: 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 CoreStoreFetchedResultsController( + context: 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 CoreStoreFetchedResultsController( + context: 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 CoreStoreFetchedResultsController( + context: 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 CoreStoreFetchedResultsController( + context: 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 CoreStoreFetchedResultsController( + context: 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 createFor(transaction: UnsafeDataTransaction, _ from: From, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController { + + return CoreStoreFetchedResultsController( + context: transaction.context, + fetchRequest: CoreStoreFetchRequest(), + from: from, + sectionBy: nil, + fetchClauses: fetchClauses + ) + } + + @available(*, deprecated=1.6.8, message="Use NSFetchedResultsController.createFor(_:_:_:_:) instead.") + @nonobjc public static func createForStack(dataStack: DataStack, fetchRequest: NSFetchRequest, from: From? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) -> NSFetchedResultsController { return CoreStoreFetchedResultsController( @@ -45,7 +207,8 @@ public extension NSFetchedResultsController { ) } - @available(*, deprecated=1.5.2, message="Use NSFetchedResultsController.createForStack(_:fetchRequest:from:sectionBy:fetchClauses:) to create NSFetchedResultsControllers directly") + @available(*, deprecated=1.5.2, message="Use NSFetchedResultsController.createFor(_:_:_:_:) to create NSFetchedResultsControllers directly") + @nonobjc public convenience init(dataStack: DataStack, fetchRequest: NSFetchRequest, from: From? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) { let context = dataStack.mainContext diff --git a/CoreStore/Info.plist b/CoreStore/Info.plist index 9283e2f..e09a8cf 100644 --- a/CoreStore/Info.plist +++ b/CoreStore/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.6.7 + 1.6.8 CFBundleSignature ???? CFBundleVersion diff --git a/CoreStore/Internal/CoreStoreFetchedResultsController.swift b/CoreStore/Internal/CoreStoreFetchedResultsController.swift index 1286713..de712a1 100644 --- a/CoreStore/Internal/CoreStoreFetchedResultsController.swift +++ b/CoreStore/Internal/CoreStoreFetchedResultsController.swift @@ -48,6 +48,10 @@ internal final class CoreStoreFetchedResultsController: NSFe internal init(context: NSManagedObjectContext, fetchRequest: NSFetchRequest, from: From? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) { + CoreStore.assert( + fetchClauses.filter { $0 is OrderBy }.count > 0, + "An \(typeName(NSFetchedResultsController)) requires an OrderBy clause." + ) from?.applyToFetchRequest(fetchRequest, context: context, applyAffectedStores: false) for clause in fetchClauses {