diff --git a/CoreStore/Convenience Helpers/NSFetchedResultsController+Convenience.swift b/CoreStore/Convenience Helpers/NSFetchedResultsController+Convenience.swift index ef0d0d6..6dd1452 100644 --- a/CoreStore/Convenience Helpers/NSFetchedResultsController+Convenience.swift +++ b/CoreStore/Convenience Helpers/NSFetchedResultsController+Convenience.swift @@ -34,7 +34,7 @@ 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`. */ - public func createForStack(dataStack: DataStack, fetchRequest: NSFetchRequest, from: From? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) -> NSFetchedResultsController { + public static func createForStack(dataStack: DataStack, fetchRequest: NSFetchRequest, from: From? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) -> NSFetchedResultsController { return CoreStoreFetchedResultsController( context: dataStack.mainContext, @@ -45,10 +45,41 @@ public extension NSFetchedResultsController { ) } + @available(*, deprecated=1.5.2, message="Use NSFetchedResultsController.createForStack(_:fetchRequest:from:sectionBy:fetchClauses:) to create NSFetchedResultsControllers directly") + public convenience init(dataStack: DataStack, fetchRequest: NSFetchRequest, from: From? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) { + + let context = dataStack.mainContext + from?.applyToFetchRequest(fetchRequest, context: context, applyAffectedStores: false) + for clause in fetchClauses { + + clause.applyToFetchRequest(fetchRequest) + } + + if let from = from { + + from.applyAffectedStoresForFetchedRequest(fetchRequest, context: context) + } + else { + + guard let from = (fetchRequest.entity.flatMap { $0.managedObjectClassName }).flatMap(NSClassFromString).flatMap(From.init) else { + + fatalError("Attempted to create an \(typeName(NSFetchedResultsController)) without a From clause or an NSEntityDescription.") + } + from.applyAffectedStoresForFetchedRequest(fetchRequest, context: context) + } + + self.init( + fetchRequest: fetchRequest, + managedObjectContext: context, + sectionNameKeyPath: sectionBy?.sectionKeyPath, + cacheName: nil + ) + } + // MARK: Internal - internal func createFromContext(context: NSManagedObjectContext, fetchRequest: NSFetchRequest, from: From? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) -> NSFetchedResultsController { + internal static func createFromContext(context: NSManagedObjectContext, fetchRequest: NSFetchRequest, from: From? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) -> NSFetchedResultsController { return CoreStoreFetchedResultsController( context: context, diff --git a/CoreStore/Saving and Processing/SynchronousDataTransaction.swift b/CoreStore/Saving and Processing/SynchronousDataTransaction.swift index bb6404e..2f61bed 100644 --- a/CoreStore/Saving and Processing/SynchronousDataTransaction.swift +++ b/CoreStore/Saving and Processing/SynchronousDataTransaction.swift @@ -39,8 +39,10 @@ public final class SynchronousDataTransaction: BaseDataTransaction { /** Saves the transaction changes and waits for completion synchronously. This method should not be used after the `commit()` method was already called once. + + - returns: a `SaveResult` containing the success or failure information */ - public func commit() { + public func commitAndWait() -> SaveResult { CoreStore.assert( self.transactionQueue.isCurrentExecutionContext(), @@ -52,7 +54,10 @@ public final class SynchronousDataTransaction: BaseDataTransaction { ) self.isCommitted = true - self.result = self.context.saveSynchronously() + + let result = self.context.saveSynchronously() + self.result = result + return result } /** @@ -197,6 +202,12 @@ public final class SynchronousDataTransaction: BaseDataTransaction { self.context.reset() } + @available(*, deprecated=1.5.2, renamed="commitAndWait") + public func commit() { + + self.commitAndWait() + } + // MARK: Internal diff --git a/CoreStore/Saving and Processing/UnsafeDataTransaction.swift b/CoreStore/Saving and Processing/UnsafeDataTransaction.swift index 26224c7..f18bad9 100644 --- a/CoreStore/Saving and Processing/UnsafeDataTransaction.swift +++ b/CoreStore/Saving and Processing/UnsafeDataTransaction.swift @@ -55,6 +55,18 @@ public final class UnsafeDataTransaction: BaseDataTransaction { } } + /** + Saves the transaction changes and waits for completion synchronously. For a `UnsafeDataTransaction`, multiple commits are allowed, although it is the developer's responsibility to ensure a reasonable leeway to prevent blocking the main thread. + + - returns: a `SaveResult` containing the success or failure information + */ + public func commitAndWait() -> SaveResult { + + let result = self.context.saveSynchronously() + self.result = result + return result + } + /** Rolls back the transaction. */ diff --git a/README.md b/README.md index b23449e..baa2624 100644 --- a/README.md +++ b/README.md @@ -260,9 +260,9 @@ do { completion: { (result) -> Void in switch result { case .Success(let persistentStore): - print("Successfully added sqlite store: \(persistentStore)" + print("Successfully added sqlite store: \(persistentStore)") case .Failure(let error): - print("Failed adding sqlite store with error: \(error)" + print("Failed adding sqlite store with error: \(error)") } } )