Improve CoreStoreObjects KVO behavior

This commit is contained in:
John Rommel Estropia
2017-06-10 21:02:36 +09:00
parent 997c5bdcfa
commit fe135acbec
7 changed files with 743 additions and 294 deletions

View File

@@ -13,35 +13,15 @@ import CoreData
@objc internal class CoreStoreManagedObject: NSManagedObject {
internal typealias CustomGetter = @convention(block) (_ rawObject: Any) -> Any?
internal typealias CustomSetter = @convention(block) (_ rawObject: Any, _ newValue: Any?) -> Void
internal typealias CustomGetterSetter = (getter: CustomGetter?, setter: CustomSetter?)
@nonobjc @inline(__always)
internal static func cs_subclassName(for entity: DynamicEntity, in modelVersion: ModelVersion) -> String {
return "_\(NSStringFromClass(CoreStoreManagedObject.self))__\(modelVersion)__\(NSStringFromClass(entity.type))__\(entity.entityName)"
}
@nonobjc
internal class func cs_setKeyPathsForValuesAffectingKeys(_ keyPathsForValuesAffectingKeys: [KeyPath: Set<KeyPath>], for managedObjectClass: CoreStoreManagedObject.Type) {
Static.queue.sync(flags: .barrier) {
Static.cache[ObjectIdentifier(managedObjectClass)] = keyPathsForValuesAffectingKeys
}
}
// MARK: NSManagedObject
override class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> {
return Static.queue.sync(flags: .barrier) {
let cacheKey = ObjectIdentifier(self)
if let keyPathsForValuesAffectingKeys = Static.cache[cacheKey] {
return keyPathsForValuesAffectingKeys[key] ?? []
}
return super.keyPathsForValuesAffectingValue(forKey: key)
}
}
}