From 910b5039fd9d3aa05efe74570da3a8bd925b9ff4 Mon Sep 17 00:00:00 2001 From: Andrii Chernenko Date: Fri, 7 Oct 2016 15:26:45 +0200 Subject: [PATCH] fix type method dispatch when importing objects --- .../BaseDataTransaction+Importing.swift | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/Sources/Importing/BaseDataTransaction+Importing.swift b/Sources/Importing/BaseDataTransaction+Importing.swift index f1ff670..fdc9aed 100644 --- a/Sources/Importing/BaseDataTransaction+Importing.swift +++ b/Sources/Importing/BaseDataTransaction+Importing.swift @@ -50,7 +50,9 @@ public extension BaseDataTransaction { return try autoreleasepool { - guard T.shouldInsert(from: source, in: self) else { + let entityType = into.entityClass as! T.Type + + guard entityType.shouldInsert(from: source, in: self) else { return nil } @@ -78,8 +80,10 @@ public extension BaseDataTransaction { ) try autoreleasepool { - - guard T.shouldInsert(from: source, in: self) else { + + let entityType = type(of: object) + + guard entityType.shouldInsert(from: source, in: self) else { return } @@ -108,8 +112,10 @@ public extension BaseDataTransaction { return try autoreleasepool { return try sourceArray.flatMap { (source) -> T? in - - guard T.shouldInsert(from: source, in: self) else { + + let entityType = into.entityClass as! T.Type + + guard entityType.shouldInsert(from: source, in: self) else { return nil } @@ -142,16 +148,18 @@ public extension BaseDataTransaction { ) return try autoreleasepool { - - let uniqueIDKeyPath = T.uniqueIDKeyPath - guard let uniqueIDValue = try T.uniqueID(from: source, in: self) else { + + let entityType = into.entityClass as! T.Type + + let uniqueIDKeyPath = entityType.uniqueIDKeyPath + guard let uniqueIDValue = try entityType.uniqueID(from: source, in: self) else { return nil } - if let object = self.fetchOne(From(), Where(uniqueIDKeyPath, isEqualTo: uniqueIDValue)) { + if let object = self.fetchOne(From(entityType), Where(uniqueIDKeyPath, isEqualTo: uniqueIDValue)) { - guard T.shouldUpdate(from: source, in: self) else { + guard entityType.shouldUpdate(from: source, in: self) else { return nil } @@ -161,7 +169,7 @@ public extension BaseDataTransaction { } else { - guard T.shouldInsert(from: source, in: self) else { + guard entityType.shouldInsert(from: source, in: self) else { return nil } @@ -195,13 +203,15 @@ public extension BaseDataTransaction { ) return try autoreleasepool { - + + let entityType = into.entityClass as! T.Type + var mapping = Dictionary() let sortedIDs = try autoreleasepool { - + return try sourceArray.flatMap { (source) -> T.UniqueIDType? in - guard let uniqueIDValue = try T.uniqueID(from: source, in: self) else { + guard let uniqueIDValue = try entityType.uniqueID(from: source, in: self) else { return nil } @@ -214,14 +224,14 @@ public extension BaseDataTransaction { mapping = try autoreleasepool { try preProcess(mapping) } var objects = Dictionary() - for object in self.fetchAll(From(), Where(T.uniqueIDKeyPath, isMemberOf: sortedIDs)) ?? [] { + for object in self.fetchAll(From(entityType), Where(entityType.uniqueIDKeyPath, isMemberOf: sortedIDs)) ?? [] { try autoreleasepool { let uniqueIDValue = object.uniqueIDValue guard let source = mapping.removeValue(forKey: uniqueIDValue), - T.shouldUpdate(from: source, in: self) else { + entityType.shouldUpdate(from: source, in: self) else { return } @@ -235,7 +245,7 @@ public extension BaseDataTransaction { try autoreleasepool { - guard T.shouldInsert(from: source, in: self) else { + guard entityType.shouldInsert(from: source, in: self) else { return }