mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-21 00:49:49 +01:00
Revert "Import unique objects in the same order as the array of import sources"
This commit is contained in:
@@ -184,8 +184,7 @@ public extension BaseDataTransaction {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
Updates existing `ImportableUniqueObject`s or creates them by importing from the specified array of import sources.
|
Updates existing `ImportableUniqueObject`s or creates them by importing from the specified array of import sources.
|
||||||
Objects are called with `ImportableUniqueObject` methods in the same order as in import sources array.
|
- Warning: While the array returned from `importUniqueObjects(...)` correctly maps to the order of `sourceArray`, the order of objects called with `ImportableUniqueObject` methods is arbitrary. Do not make assumptions that any particular object will be imported ahead or after another object.
|
||||||
The array returned from `importUniqueObjects(...)` correctly maps to the order of `sourceArray`.
|
|
||||||
|
|
||||||
- parameter into: an `Into` clause specifying the entity type
|
- parameter into: an `Into` clause specifying the entity type
|
||||||
- parameter sourceArray: the array of objects to import values from
|
- parameter sourceArray: the array of objects to import values from
|
||||||
@@ -207,8 +206,7 @@ public extension BaseDataTransaction {
|
|||||||
|
|
||||||
let entityType = into.entityClass as! T.Type
|
let entityType = into.entityClass as! T.Type
|
||||||
|
|
||||||
var importSourceByID = Dictionary<T.UniqueIDType, T.ImportSource>()
|
var mapping = Dictionary<T.UniqueIDType, T.ImportSource>()
|
||||||
|
|
||||||
let sortedIDs = try autoreleasepool {
|
let sortedIDs = try autoreleasepool {
|
||||||
|
|
||||||
return try sourceArray.flatMap { (source) -> T.UniqueIDType? in
|
return try sourceArray.flatMap { (source) -> T.UniqueIDType? in
|
||||||
@@ -218,44 +216,49 @@ public extension BaseDataTransaction {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
importSourceByID[uniqueIDValue] = source
|
mapping[uniqueIDValue] = source
|
||||||
return uniqueIDValue
|
return uniqueIDValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
importSourceByID = try autoreleasepool { try preProcess(importSourceByID) }
|
mapping = try autoreleasepool { try preProcess(mapping) }
|
||||||
|
|
||||||
|
var objects = Dictionary<T.UniqueIDType, T>()
|
||||||
var existingObjectsByID = Dictionary<T.UniqueIDType, T>()
|
for object in self.fetchAll(From(entityType), Where(entityType.uniqueIDKeyPath, isMemberOf: sortedIDs)) ?? [] {
|
||||||
self.fetchAll(From(entityType), Where(entityType.uniqueIDKeyPath, isMemberOf: sortedIDs))?
|
|
||||||
.forEach { existingObjectsByID[$0.uniqueIDValue] = $0 }
|
|
||||||
|
|
||||||
var insertedObjectsByID = Dictionary<T.UniqueIDType, T>()
|
|
||||||
|
|
||||||
for objectID in sortedIDs {
|
|
||||||
|
|
||||||
try autoreleasepool {
|
try autoreleasepool {
|
||||||
|
|
||||||
guard let source = importSourceByID[objectID] else { return }
|
let uniqueIDValue = object.uniqueIDValue
|
||||||
|
|
||||||
|
guard let source = mapping.removeValue(forKey: uniqueIDValue),
|
||||||
|
entityType.shouldUpdate(from: source, in: self) else {
|
||||||
|
|
||||||
if let object = existingObjectsByID[objectID] {
|
return
|
||||||
guard entityType.shouldUpdate(from: source, in: self) else { return }
|
|
||||||
|
|
||||||
try object.update(from: source, in: self)
|
|
||||||
}
|
}
|
||||||
else if entityType.shouldInsert(from: source, in: self) {
|
|
||||||
let object = self.create(into)
|
|
||||||
object.uniqueIDValue = objectID
|
|
||||||
try object.didInsert(from: source, in: self)
|
|
||||||
|
|
||||||
insertedObjectsByID[objectID] = object
|
try object.update(from: source, in: self)
|
||||||
}
|
objects[uniqueIDValue] = object
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (uniqueIDValue, source) in mapping {
|
||||||
|
|
||||||
return sortedIDs.flatMap { existingObjectsByID[$0] ?? insertedObjectsByID[$0] }
|
try autoreleasepool {
|
||||||
|
|
||||||
|
guard entityType.shouldInsert(from: source, in: self) else {
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let object = self.create(into)
|
||||||
|
object.uniqueIDValue = uniqueIDValue
|
||||||
|
try object.didInsert(from: source, in: self)
|
||||||
|
|
||||||
|
objects[uniqueIDValue] = object
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sortedIDs.flatMap { objects[$0] }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user