performant access of relationship objectIDs for snapshots

This commit is contained in:
John Estropia
2020-02-21 13:51:17 +09:00
parent 361dba58c6
commit 231e138ab0
10 changed files with 121 additions and 89 deletions

View File

@@ -99,7 +99,11 @@ extension FieldContainer {
instance.rawObject != nil,
"Attempted to access values from a \(Internals.typeName(O.self)) meta object. Meta objects are only used for querying keyPaths and infering types."
)
return self.read(field: instance[keyPath: storageKeyPath], for: instance.rawObject!, bypassThreadCheck: false) as! V
Internals.assert(
instance.rawObject?.isRunningInAllowedQueue() == true,
"Attempted to access \(Internals.typeName(O.self))'s value outside it's designated queue."
)
return self.read(field: instance[keyPath: storageKeyPath], for: instance.rawObject!) as! V
}
set {
@@ -107,6 +111,10 @@ extension FieldContainer {
instance.rawObject != nil,
"Attempted to access values from a \(Internals.typeName(O.self)) meta object. Meta objects are only used for querying keyPaths and infering types."
)
Internals.assert(
instance.rawObject?.isRunningInAllowedQueue() == true,
"Attempted to access \(Internals.typeName(O.self))'s value outside it's designated queue."
)
return self.modify(field: instance[keyPath: storageKeyPath], for: instance.rawObject!, newValue: newValue)
}
}
@@ -138,12 +146,13 @@ extension FieldContainer {
// MARK: FieldProtocol
internal static func read(field: FieldProtocol, for rawObject: CoreStoreManagedObject, bypassThreadCheck: Bool) -> Any? {
internal static var dynamicObjectType: CoreStoreObject.Type {
return ObjectType.self
}
internal static func read(field: FieldProtocol, for rawObject: CoreStoreManagedObject) -> Any? {
Internals.assert(
bypassThreadCheck || rawObject.isRunningInAllowedQueue() == true,
"Attempted to access \(Internals.typeName(O.self))'s value outside it's designated queue."
)
let field = field as! Self
let keyPath = field.keyPath
return V.cs_toReturnType(
@@ -152,11 +161,7 @@ extension FieldContainer {
}
internal static func modify(field: FieldProtocol, for rawObject: CoreStoreManagedObject, newValue: Any?) {
Internals.assert(
rawObject.isRunningInAllowedQueue() == true,
"Attempted to access \(Internals.typeName(O.self))'s value outside it's designated queue."
)
Internals.assert(
rawObject.isEditableInContext() == true,
"Attempted to update a \(Internals.typeName(O.self))'s value from outside a transaction."
@@ -182,10 +187,7 @@ extension FieldContainer {
"Attempted to access \(Internals.typeName(O.self))'s value outside it's designated queue."
)
let field = field as! Self
let keyPath = field.keyPath
return V.cs_valueForSnapshot(
from: rawObject.value(forKey: keyPath) as! V.NativeValueType?
)
return V.cs_valueForSnapshot(from: rawObject.objectIDs(forRelationshipNamed: field.keyPath))
}