diff --git a/CoreStore.podspec b/CoreStore.podspec index 66530b6..5395e1b 100644 --- a/CoreStore.podspec +++ b/CoreStore.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "CoreStore" - s.version = "1.3.0" + s.version = "1.3.1" s.license = "MIT" s.summary = "Simple, elegant, and smart Core Data programming with Swift" s.homepage = "https://github.com/JohnEstropia/CoreStore" diff --git a/CoreStore/Fetching and Querying/BaseDataTransaction+Querying.swift b/CoreStore/Fetching and Querying/BaseDataTransaction+Querying.swift index ef01611..44496d9 100644 --- a/CoreStore/Fetching and Querying/BaseDataTransaction+Querying.swift +++ b/CoreStore/Fetching and Querying/BaseDataTransaction+Querying.swift @@ -78,17 +78,9 @@ public extension BaseDataTransaction { - returns: the `NSManagedObject` array for objects that exists in the transaction */ @warn_unused_result - public func fetchExisting(objects: [T]) -> [T] { + public func fetchExisting(objects: S) -> [T] { - var existingObjects = [T]() - for object in objects { - - if let existingObject = (try? self.context.existingObjectWithID(object.objectID)) as? T { - - existingObjects.append(existingObject) - } - } - return existingObjects + return objects.flatMap { (try? self.context.existingObjectWithID($0.objectID)) as? T } } /** @@ -98,17 +90,9 @@ public extension BaseDataTransaction { - returns: the `NSManagedObject` array for objects that exists in the transaction */ @warn_unused_result - public func fetchExisting(objectIDs: [NSManagedObjectID]) -> [T] { + public func fetchExisting(objectIDs: S) -> [T] { - var existingObjects = [T]() - for objectID in objectIDs { - - if let existingObject = (try? self.context.existingObjectWithID(objectID)) as? T { - - existingObjects.append(existingObject) - } - } - return existingObjects + return objectIDs.flatMap { (try? self.context.existingObjectWithID($0)) as? T } } /** diff --git a/CoreStore/Fetching and Querying/CoreStore+Querying.swift b/CoreStore/Fetching and Querying/CoreStore+Querying.swift index 3fb065c..05ffee5 100644 --- a/CoreStore/Fetching and Querying/CoreStore+Querying.swift +++ b/CoreStore/Fetching and Querying/CoreStore+Querying.swift @@ -62,7 +62,7 @@ public extension CoreStore { - returns: the `NSManagedObject` array for objects that exists in the `DataStack` */ @warn_unused_result - public static func fetchExisting(objects: [T]) -> [T] { + public static func fetchExisting(objects: S) -> [T] { return self.defaultStack.fetchExisting(objects) } @@ -74,7 +74,7 @@ public extension CoreStore { - returns: the `NSManagedObject` array for objects that exists in the `DataStack` */ @warn_unused_result - public static func fetchExisting(objectIDs: [NSManagedObjectID]) -> [T] { + public static func fetchExisting(objectIDs: S) -> [T] { return self.defaultStack.fetchExisting(objectIDs) } diff --git a/CoreStore/Fetching and Querying/DataStack+Querying.swift b/CoreStore/Fetching and Querying/DataStack+Querying.swift index 7c77842..5408b98 100644 --- a/CoreStore/Fetching and Querying/DataStack+Querying.swift +++ b/CoreStore/Fetching and Querying/DataStack+Querying.swift @@ -79,17 +79,9 @@ public extension DataStack { - returns: the `NSManagedObject` array for objects that exists in the `DataStack` */ @warn_unused_result - public func fetchExisting(objects: [T]) -> [T] { + public func fetchExisting(objects: S) -> [T] { - var existingObjects = [T]() - for object in objects { - - if let existingObject = (try? self.mainContext.existingObjectWithID(object.objectID)) as? T { - - existingObjects.append(existingObject) - } - } - return existingObjects + return objects.flatMap { (try? self.mainContext.existingObjectWithID($0.objectID)) as? T } } /** @@ -99,17 +91,9 @@ public extension DataStack { - returns: the `NSManagedObject` array for objects that exists in the `DataStack` */ @warn_unused_result - public func fetchExisting(objectIDs: [NSManagedObjectID]) -> [T] { + public func fetchExisting(objectIDs: S) -> [T] { - var existingObjects = [T]() - for objectID in objectIDs { - - if let existingObject = (try? self.mainContext.existingObjectWithID(objectID)) as? T { - - existingObjects.append(existingObject) - } - } - return existingObjects + return objectIDs.flatMap { (try? self.mainContext.existingObjectWithID($0)) as? T } } /** diff --git a/CoreStore/Importing Data/BaseDataTransaction+Importing.swift b/CoreStore/Importing Data/BaseDataTransaction+Importing.swift index a42ae0a..1d2f679 100644 --- a/CoreStore/Importing Data/BaseDataTransaction+Importing.swift +++ b/CoreStore/Importing Data/BaseDataTransaction+Importing.swift @@ -102,9 +102,9 @@ public extension BaseDataTransaction { - parameter sourceArray: the array of objects to import values from - parameter postProcess: a closure that exposes the array of created objects */ - public func importObjects( + public func importObjects( into: Into, - sourceArray: [T.ImportSource], + sourceArray: S, @noescape postProcess: (sorted: [T]) -> Void) throws { CoreStore.assert( @@ -185,10 +185,10 @@ public extension BaseDataTransaction { - parameter sourceArray: the array of objects to import values from - parameter preProcess: a closure that lets the caller tweak the internal `UniqueIDType`-to-`ImportSource` mapping to be used for importing. Callers can remove from/add to/update `mapping` and return the updated array from the closure. */ - public func importUniqueObjects( + public func importUniqueObjects( into: Into, - sourceArray: [T.ImportSource], - preProcess: ((mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource])? = nil) throws { + sourceArray: S, + @noescape preProcess: (mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource] = { $0 }) throws { CoreStore.assert( self.bypassesQueueing || self.transactionQueue.isCurrentExecutionContext(), @@ -211,13 +211,7 @@ public extension BaseDataTransaction { } } - if let preProcess = preProcess { - - try autoreleasepool { - - mapping = try preProcess(mapping: mapping) - } - } + mapping = try autoreleasepool { try preProcess(mapping: mapping) } for object in self.fetchAll(From(T), Where(T.uniqueIDKeyPath, isMemberOf: mapping.keys)) ?? [] { @@ -260,10 +254,10 @@ public extension BaseDataTransaction { - parameter preProcess: a closure that lets the caller tweak the internal `UniqueIDType`-to-`ImportSource` mapping to be used for importing. Callers can remove from/add to/update `mapping` and return the updated array from the closure. - parameter postProcess: a closure that exposes the array of created/updated objects */ - public func importUniqueObjects( + public func importUniqueObjects( into: Into, - sourceArray: [T.ImportSource], - preProcess: ((mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource])? = nil, + sourceArray: S, + @noescape preProcess: (mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource] = { $0 }, @noescape postProcess: (sorted: [T]) -> Void) throws { CoreStore.assert( @@ -289,13 +283,7 @@ public extension BaseDataTransaction { } } - if let preProcess = preProcess { - - try autoreleasepool { - - mapping = try preProcess(mapping: mapping) - } - } + mapping = try autoreleasepool { try preProcess(mapping: mapping) } var objects = Dictionary() for object in self.fetchAll(From(T), Where(T.uniqueIDKeyPath, isMemberOf: mapping.keys)) ?? [] { diff --git a/CoreStore/Info.plist b/CoreStore/Info.plist index 9f4a4aa..aa82cca 100644 --- a/CoreStore/Info.plist +++ b/CoreStore/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.3.0 + 1.3.1 CFBundleSignature ???? CFBundleVersion diff --git a/CoreStore/Internal/Functions.swift b/CoreStore/Internal/Functions.swift index 6fbba1d..e4d3b3a 100644 --- a/CoreStore/Internal/Functions.swift +++ b/CoreStore/Internal/Functions.swift @@ -25,9 +25,9 @@ import Foundation -internal func autoreleasepool(@noescape closure: () -> T?) -> T? { +internal func autoreleasepool(@noescape closure: () -> T) -> T { - var closureValue: T? + var closureValue: T! ObjectiveC.autoreleasepool { closureValue = closure() @@ -36,9 +36,9 @@ internal func autoreleasepool(@noescape closure: () -> T?) -> T? { return closureValue } -internal func autoreleasepool(@noescape closure: () throws -> T?) throws -> T? { +internal func autoreleasepool(@noescape closure: () throws -> T) throws -> T { - var closureValue: T? + var closureValue: T! var closureError: ErrorType? ObjectiveC.autoreleasepool { diff --git a/CoreStore/Saving and Processing/AsynchronousDataTransaction.swift b/CoreStore/Saving and Processing/AsynchronousDataTransaction.swift index f6c8591..8bcabed 100644 --- a/CoreStore/Saving and Processing/AsynchronousDataTransaction.swift +++ b/CoreStore/Saving and Processing/AsynchronousDataTransaction.swift @@ -171,7 +171,7 @@ public final class AsynchronousDataTransaction: BaseDataTransaction { "Attempted to delete an entities from an already committed \(typeName(self))." ) - super.delete([object1, object2] + objects) + super.delete(([object1, object2] + objects).flatMap { $0 }) } /** @@ -179,7 +179,7 @@ public final class AsynchronousDataTransaction: BaseDataTransaction { - parameter objects: the `NSManagedObject`s type to be deleted */ - public override func delete(objects: [NSManagedObject?]) { + public override func delete(objects: S) { CoreStore.assert( !self.isCommitted, diff --git a/CoreStore/Saving and Processing/BaseDataTransaction.swift b/CoreStore/Saving and Processing/BaseDataTransaction.swift index 38b1e0b..f5269d6 100644 --- a/CoreStore/Saving and Processing/BaseDataTransaction.swift +++ b/CoreStore/Saving and Processing/BaseDataTransaction.swift @@ -167,7 +167,7 @@ public /*abstract*/ class BaseDataTransaction { */ public func delete(object1: NSManagedObject?, _ object2: NSManagedObject?, _ objects: NSManagedObject?...) { - self.delete([object1, object2] + objects) + self.delete(([object1, object2] + objects).flatMap { $0 }) } /** @@ -175,7 +175,7 @@ public /*abstract*/ class BaseDataTransaction { - parameter objects: the `NSManagedObject`s to be deleted */ - public func delete(objects: [NSManagedObject?]) { + public func delete(objects: S) { CoreStore.assert( self.bypassesQueueing || self.transactionQueue.isCurrentExecutionContext(), @@ -183,10 +183,7 @@ public /*abstract*/ class BaseDataTransaction { ) let context = self.context - for case let object? in objects { - - context.fetchExisting(object)?.deleteFromContext() - } + objects.forEach { context.fetchExisting($0)?.deleteFromContext() } } // MARK: Saving changes diff --git a/CoreStore/Saving and Processing/SynchronousDataTransaction.swift b/CoreStore/Saving and Processing/SynchronousDataTransaction.swift index 0c62795..16023c0 100644 --- a/CoreStore/Saving and Processing/SynchronousDataTransaction.swift +++ b/CoreStore/Saving and Processing/SynchronousDataTransaction.swift @@ -161,7 +161,7 @@ public final class SynchronousDataTransaction: BaseDataTransaction { "Attempted to delete an entities from an already committed \(typeName(self))." ) - super.delete([object1, object2] + objects) + super.delete(([object1, object2] + objects).flatMap { $0 }) } /** @@ -169,7 +169,7 @@ public final class SynchronousDataTransaction: BaseDataTransaction { - parameter objects: the `NSManagedObject`s to be deleted */ - public override func delete(objects: [NSManagedObject?]) { + public override func delete(objects: S) { CoreStore.assert( !self.isCommitted,