From 622c5aa652b817b595bd76da62ad67c12147ca1f Mon Sep 17 00:00:00 2001 From: John Rommel Estropia Date: Sat, 19 Sep 2015 19:45:01 +0900 Subject: [PATCH] Tighter generics implementations. You can now pass any SequenceType's for methods that previously only accepts Array's. --- .../BaseDataTransaction+Importing.swift | 137 ++---------------- 1 file changed, 15 insertions(+), 122 deletions(-) diff --git a/CoreStore/Importing Data/BaseDataTransaction+Importing.swift b/CoreStore/Importing Data/BaseDataTransaction+Importing.swift index 1d2f679..7493d12 100644 --- a/CoreStore/Importing Data/BaseDataTransaction+Importing.swift +++ b/CoreStore/Importing Data/BaseDataTransaction+Importing.swift @@ -67,70 +67,33 @@ public extension BaseDataTransaction { - parameter into: an `Into` clause specifying the entity type - parameter sourceArray: the array of objects to import values from - */ - public func importObjects( - into: Into, - sourceArray: [T.ImportSource]) throws { - - CoreStore.assert( - self.bypassesQueueing || self.transactionQueue.isCurrentExecutionContext(), - "Attempted to import an object of type \(typeName(into.entityClass)) outside the transaction's designated queue." - ) - - try autoreleasepool { - - for source in sourceArray { - - guard T.shouldInsertFromImportSource(source, inTransaction: self) else { - - continue - } - - try autoreleasepool { - - let object = self.create(into) - try object.didInsertFromImportSource(source, inTransaction: self) - } - } - } - } - - /** - Creates multiple `ImportableObject`s by importing from the specified array of import sources. - - - parameter into: an `Into` clause specifying the entity type - - parameter sourceArray: the array of objects to import values from - - parameter postProcess: a closure that exposes the array of created objects + - returns: the array of created `ImportableObject` instances */ public func importObjects( into: Into, - sourceArray: S, - @noescape postProcess: (sorted: [T]) -> Void) throws { + sourceArray: S) throws -> [T] { CoreStore.assert( self.bypassesQueueing || self.transactionQueue.isCurrentExecutionContext(), "Attempted to import an object of type \(typeName(into.entityClass)) outside the transaction's designated queue." ) - try autoreleasepool { + return try autoreleasepool { - var objects = [T]() - for source in sourceArray { + return try sourceArray.flatMap { (source) -> T? in guard T.shouldInsertFromImportSource(source, inTransaction: self) else { - continue + return nil } - try autoreleasepool { + return try autoreleasepool { let object = self.create(into) try object.didInsertFromImportSource(source, inTransaction: self) - - objects.append(object) + return object } } - postProcess(sorted: objects) } } @@ -184,102 +147,32 @@ public extension BaseDataTransaction { - parameter into: an `Into` clause specifying the entity type - 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. + - returns: the array of created/updated `ImportableUniqueObject` instances */ public func importUniqueObjects( into: Into, sourceArray: S, - @noescape preProcess: (mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource] = { $0 }) throws { + @noescape preProcess: (mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource] = { $0 }) throws -> [T] { CoreStore.assert( self.bypassesQueueing || self.transactionQueue.isCurrentExecutionContext(), "Attempted to import an object of type \(typeName(into.entityClass)) outside the transaction's designated queue." ) - try autoreleasepool { + return try autoreleasepool { var mapping = Dictionary() - for source in sourceArray { + let sortedIDs = try autoreleasepool { - try autoreleasepool { + return try sourceArray.flatMap { (source) -> T.UniqueIDType? in guard let uniqueIDValue = try T.uniqueIDFromImportSource(source, inTransaction: self) else { - return + return nil } mapping[uniqueIDValue] = source - } - } - - mapping = try autoreleasepool { try preProcess(mapping: mapping) } - - for object in self.fetchAll(From(T), Where(T.uniqueIDKeyPath, isMemberOf: mapping.keys)) ?? [] { - - try autoreleasepool { - - let uniqueIDValue = object.uniqueIDValue - - guard let source = mapping.removeValueForKey(uniqueIDValue) - where T.shouldUpdateFromImportSource(source, inTransaction: self) else { - - return - } - - try object.updateFromImportSource(source, inTransaction: self) - } - } - - for (uniqueIDValue, source) in mapping { - - try autoreleasepool { - - guard T.shouldInsertFromImportSource(source, inTransaction: self) else { - - return - } - - let object = self.create(into) - object.uniqueIDValue = uniqueIDValue - try object.didInsertFromImportSource(source, inTransaction: self) - } - } - } - } - - /** - Updates existing `ImportableUniqueObject`s or creates them by importing from the specified array of import sources. - - - parameter into: an `Into` clause specifying the entity type - - 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. - - parameter postProcess: a closure that exposes the array of created/updated objects - */ - public func importUniqueObjects( - into: Into, - sourceArray: S, - @noescape preProcess: (mapping: [T.UniqueIDType: T.ImportSource]) throws -> [T.UniqueIDType: T.ImportSource] = { $0 }, - @noescape postProcess: (sorted: [T]) -> Void) throws { - - CoreStore.assert( - self.bypassesQueueing || self.transactionQueue.isCurrentExecutionContext(), - "Attempted to import an object of type \(typeName(into.entityClass)) outside the transaction's designated queue." - ) - - try autoreleasepool { - - var sortedIDs = Array() - var mapping = Dictionary() - for source in sourceArray { - - try autoreleasepool { - - guard let uniqueIDValue = try T.uniqueIDFromImportSource(source, inTransaction: self) else { - - return - } - - mapping[uniqueIDValue] = source - sortedIDs.append(uniqueIDValue) + return uniqueIDValue } } @@ -320,7 +213,7 @@ public extension BaseDataTransaction { } } - postProcess(sorted: sortedIDs.flatMap { objects[$0] }) + return sortedIDs.flatMap { objects[$0] } } } }