resurrect utilities for creating NSFetchedResultsController (fixes #119)

This commit is contained in:
John Estropia
2016-11-11 17:32:13 +09:00
parent 73637321ce
commit 33a5c123aa
7 changed files with 405 additions and 244 deletions

View File

@@ -184,9 +184,8 @@ public extension BaseDataTransaction {
/**
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.
The array returned from `importUniqueObjects(...)` correctly maps to the order of `sourceArray`.
If `sourceArray` contains multiple import sources with same ID, the last one will be imported.
`ImportableUniqueObject` methods are called on the objects in the same order as they are in the `sourceArray`, and are returned in an array with that same order.
- Warning: If `sourceArray` contains multiple import sources with same ID, no merging will occur and ONLY THE LAST duplicate will be imported.
- parameter into: an `Into` clause specifying the entity type
- parameter sourceArray: the array of objects to import values from
@@ -217,8 +216,7 @@ public extension BaseDataTransaction {
return nil
}
// each subsequent import source with the same ID will replace the existing one
importSourceByID[uniqueIDValue] = source
importSourceByID[uniqueIDValue] = source // effectively replaces duplicate with the latest
return uniqueIDValue
}
}
@@ -227,17 +225,19 @@ public extension BaseDataTransaction {
var existingObjectsByID = Dictionary<T.UniqueIDType, T>()
self.fetchAll(From(entityType), Where(entityType.uniqueIDKeyPath, isMemberOf: sortedIDs))?
.forEach { existingObjectsByID[$0.uniqueIDValue] = $0 }
.forEach { existingObjectsByID[$0.uniqueIDValue] = $0 }
var processedObjectIDs = Set<T.UniqueIDType>()
var result = [T]()
for objectID in sortedIDs {
for objectID in sortedIDs where !processedObjectIDs.contains(objectID) {
guard let source = importSourceByID[objectID] else {
continue
}
try autoreleasepool {
guard let source = importSourceByID[objectID], !processedObjectIDs.contains(objectID) else { return }
if let object = existingObjectsByID[objectID] {
guard entityType.shouldUpdate(from: source, in: self) else { return }
@@ -256,7 +256,6 @@ public extension BaseDataTransaction {
processedObjectIDs.insert(objectID)
}
}
return result
}
}