mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-04-01 23:23:07 +02:00
rewrote ImportableObject protocol methods
This commit is contained in:
@@ -31,24 +31,17 @@ public protocol ImportableObject: class {
|
|||||||
|
|
||||||
typealias ImportSource
|
typealias ImportSource
|
||||||
|
|
||||||
static func shouldImportFromSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool
|
static func shouldInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool
|
||||||
|
|
||||||
func didInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws
|
func didInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws
|
||||||
|
|
||||||
func updateFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public extension ImportableObject {
|
public extension ImportableObject {
|
||||||
|
|
||||||
static func shouldImportFromSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool {
|
static func shouldInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool {
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func didInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws {
|
|
||||||
|
|
||||||
try self.updateFromImportSource(source, inTransaction: transaction)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -60,7 +53,20 @@ public protocol ImportableUniqueObject: ImportableObject {
|
|||||||
|
|
||||||
var uniqueIDValue: UniqueIDType { get set }
|
var uniqueIDValue: UniqueIDType { get set }
|
||||||
|
|
||||||
static func uniqueIDFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws -> UniqueIDType
|
static func uniqueIDFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws -> UniqueIDType?
|
||||||
|
|
||||||
|
static func shouldUpdateFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) -> Bool
|
||||||
|
|
||||||
|
func updateFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public extension ImportableUniqueObject {
|
||||||
|
|
||||||
|
func didInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws {
|
||||||
|
|
||||||
|
try self.updateFromImportSource(source, inTransaction: transaction)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -77,7 +83,7 @@ public extension BaseDataTransaction {
|
|||||||
|
|
||||||
return try autoreleasepool {
|
return try autoreleasepool {
|
||||||
|
|
||||||
if !T.shouldImportFromSource(source, inTransaction: self) {
|
guard T.shouldInsertFromImportSource(source, inTransaction: self) else {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -101,6 +107,11 @@ public extension BaseDataTransaction {
|
|||||||
|
|
||||||
for source in sourceArray {
|
for source in sourceArray {
|
||||||
|
|
||||||
|
guard T.shouldInsertFromImportSource(source, inTransaction: self) else {
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
try autoreleasepool {
|
try autoreleasepool {
|
||||||
|
|
||||||
let object = self.create(into)
|
let object = self.create(into)
|
||||||
@@ -125,6 +136,11 @@ public extension BaseDataTransaction {
|
|||||||
var objects = [T]()
|
var objects = [T]()
|
||||||
for source in sourceArray {
|
for source in sourceArray {
|
||||||
|
|
||||||
|
guard T.shouldInsertFromImportSource(source, inTransaction: self) else {
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
try autoreleasepool {
|
try autoreleasepool {
|
||||||
|
|
||||||
let object = self.create(into)
|
let object = self.create(into)
|
||||||
@@ -148,14 +164,12 @@ public extension BaseDataTransaction {
|
|||||||
|
|
||||||
return try autoreleasepool {
|
return try autoreleasepool {
|
||||||
|
|
||||||
if !T.shouldImportFromSource(source, inTransaction: self) {
|
let uniqueIDKeyPath = T.uniqueIDKeyPath
|
||||||
|
guard let uniqueIDValue = try T.uniqueIDFromImportSource(source, inTransaction: self) else {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
let uniqueIDKeyPath = T.uniqueIDKeyPath
|
|
||||||
let uniqueIDValue = try T.uniqueIDFromImportSource(source, inTransaction: self)
|
|
||||||
|
|
||||||
if let object = self.fetchOne(From(T), Where(uniqueIDKeyPath, isEqualTo: uniqueIDValue)) {
|
if let object = self.fetchOne(From(T), Where(uniqueIDKeyPath, isEqualTo: uniqueIDValue)) {
|
||||||
|
|
||||||
try object.updateFromImportSource(source, inTransaction: self)
|
try object.updateFromImportSource(source, inTransaction: self)
|
||||||
@@ -163,6 +177,11 @@ public extension BaseDataTransaction {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
|
guard T.shouldInsertFromImportSource(source, inTransaction: self) else {
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
let object = self.create(into)
|
let object = self.create(into)
|
||||||
object.uniqueIDValue = uniqueIDValue
|
object.uniqueIDValue = uniqueIDValue
|
||||||
try object.didInsertFromImportSource(source, inTransaction: self)
|
try object.didInsertFromImportSource(source, inTransaction: self)
|
||||||
@@ -188,12 +207,11 @@ public extension BaseDataTransaction {
|
|||||||
|
|
||||||
try autoreleasepool {
|
try autoreleasepool {
|
||||||
|
|
||||||
if !T.shouldImportFromSource(source, inTransaction: self) {
|
guard let uniqueIDValue = try T.uniqueIDFromImportSource(source, inTransaction: self) else {
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let uniqueIDValue = try T.uniqueIDFromImportSource(source, inTransaction: self)
|
|
||||||
mapping[uniqueIDValue] = source
|
mapping[uniqueIDValue] = source
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -211,7 +229,14 @@ public extension BaseDataTransaction {
|
|||||||
try autoreleasepool {
|
try autoreleasepool {
|
||||||
|
|
||||||
let uniqueIDValue = object.uniqueIDValue
|
let uniqueIDValue = object.uniqueIDValue
|
||||||
try object.updateFromImportSource(mapping.removeValueForKey(uniqueIDValue)!, inTransaction: self)
|
|
||||||
|
guard let source = mapping.removeValueForKey(uniqueIDValue)
|
||||||
|
where T.shouldUpdateFromImportSource(source, inTransaction: self) else {
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try object.updateFromImportSource(source, inTransaction: self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,6 +244,11 @@ public extension BaseDataTransaction {
|
|||||||
|
|
||||||
try autoreleasepool {
|
try autoreleasepool {
|
||||||
|
|
||||||
|
guard T.shouldInsertFromImportSource(source, inTransaction: self) else {
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
let object = self.create(into)
|
let object = self.create(into)
|
||||||
object.uniqueIDValue = uniqueIDValue
|
object.uniqueIDValue = uniqueIDValue
|
||||||
try object.didInsertFromImportSource(source, inTransaction: self)
|
try object.didInsertFromImportSource(source, inTransaction: self)
|
||||||
@@ -246,12 +276,11 @@ public extension BaseDataTransaction {
|
|||||||
|
|
||||||
try autoreleasepool {
|
try autoreleasepool {
|
||||||
|
|
||||||
if !T.shouldImportFromSource(source, inTransaction: self) {
|
guard let uniqueIDValue = try T.uniqueIDFromImportSource(source, inTransaction: self) else {
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let uniqueIDValue = try T.uniqueIDFromImportSource(source, inTransaction: self)
|
|
||||||
mapping[uniqueIDValue] = source
|
mapping[uniqueIDValue] = source
|
||||||
sortedIDs.append(uniqueIDValue)
|
sortedIDs.append(uniqueIDValue)
|
||||||
}
|
}
|
||||||
@@ -271,7 +300,14 @@ public extension BaseDataTransaction {
|
|||||||
try autoreleasepool {
|
try autoreleasepool {
|
||||||
|
|
||||||
let uniqueIDValue = object.uniqueIDValue
|
let uniqueIDValue = object.uniqueIDValue
|
||||||
try object.updateFromImportSource(mapping.removeValueForKey(uniqueIDValue)!, inTransaction: self)
|
|
||||||
|
guard let source = mapping.removeValueForKey(uniqueIDValue)
|
||||||
|
where T.shouldUpdateFromImportSource(source, inTransaction: self) else {
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try object.updateFromImportSource(source, inTransaction: self)
|
||||||
objects[uniqueIDValue] = object
|
objects[uniqueIDValue] = object
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -280,6 +316,11 @@ public extension BaseDataTransaction {
|
|||||||
|
|
||||||
try autoreleasepool {
|
try autoreleasepool {
|
||||||
|
|
||||||
|
guard T.shouldInsertFromImportSource(source, inTransaction: self) else {
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
let object = self.create(into)
|
let object = self.create(into)
|
||||||
object.uniqueIDValue = uniqueIDValue
|
object.uniqueIDValue = uniqueIDValue
|
||||||
try object.didInsertFromImportSource(source, inTransaction: self)
|
try object.didInsertFromImportSource(source, inTransaction: self)
|
||||||
|
|||||||
@@ -129,13 +129,13 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
|||||||
let previousCommitedAttributes = strongSelf.lastCommittedAttributes
|
let previousCommitedAttributes = strongSelf.lastCommittedAttributes
|
||||||
let currentCommitedAttributes = object.committedValuesForKeys(nil) as! [String: NSObject]
|
let currentCommitedAttributes = object.committedValuesForKeys(nil) as! [String: NSObject]
|
||||||
|
|
||||||
let changedKeys = currentCommitedAttributes.keys.reduce(Set<String>()) { (var changedKeys, key) -> Set<String> in
|
var changedKeys = Set<String>()
|
||||||
|
for key in currentCommitedAttributes.keys {
|
||||||
|
|
||||||
if previousCommitedAttributes[key] != currentCommitedAttributes[key] {
|
if previousCommitedAttributes[key] != currentCommitedAttributes[key] {
|
||||||
|
|
||||||
changedKeys.insert(key)
|
changedKeys.insert(key)
|
||||||
}
|
}
|
||||||
return changedKeys
|
|
||||||
}
|
}
|
||||||
|
|
||||||
strongSelf.lastCommittedAttributes = currentCommitedAttributes
|
strongSelf.lastCommittedAttributes = currentCommitedAttributes
|
||||||
|
|||||||
Reference in New Issue
Block a user