optimize fetching objects with NSManagedObjectIDs

This commit is contained in:
John Rommel Estropia
2015-08-26 23:59:18 +09:00
parent 10e0cf8d2c
commit 16aabe1f3b
8 changed files with 67 additions and 93 deletions

View File

@@ -110,8 +110,11 @@ public /*abstract*/ class BaseDataTransaction {
self.bypassesQueueing || self.transactionQueue.isCurrentExecutionContext(),
"Attempted to update an entity of type \(typeName(object)) outside its designated queue."
)
return object?.inContext(self.context)
guard let object = object else {
return nil
}
return self.context.fetchExisting(object)
}
/**
@@ -132,8 +135,7 @@ public /*abstract*/ class BaseDataTransaction {
|| (into.configuration ?? Into.defaultConfigurationName) == objectID.persistentStore?.configurationName,
"Attempted to update an entity of type \(typeName(T)) but the specified persistent store do not match the `NSManagedObjectID`."
)
return (into.entityClass as! NSManagedObject.Type).inContext(self.context, withObjectID: objectID) as? T
return self.fetchExisting(objectID) as? T
}
/**
@@ -147,8 +149,11 @@ public /*abstract*/ class BaseDataTransaction {
self.bypassesQueueing || self.transactionQueue.isCurrentExecutionContext(),
"Attempted to delete an entity outside its designated queue."
)
object?.inContext(self.context)?.deleteFromContext()
guard let object = object else {
return
}
self.context.fetchExisting(object)?.deleteFromContext()
}
/**
@@ -176,9 +181,9 @@ public /*abstract*/ class BaseDataTransaction {
)
let context = self.context
for object in objects {
for case let object? in objects {
object?.inContext(context)?.deleteFromContext()
context.fetchExisting(object)?.deleteFromContext()
}
}