diff --git a/Sources/NSManagedObject+Convenience.swift b/Sources/NSManagedObject+Convenience.swift index c7b6d3a..f666ce5 100644 --- a/Sources/NSManagedObject+Convenience.swift +++ b/Sources/NSManagedObject+Convenience.swift @@ -149,6 +149,25 @@ public extension NSManagedObject { self.setPrimitiveValue(value, forKey: KVCKey) } + /** + Provides a convenience wrapper for setting `setPrimitiveValue(_:forKey:)` with proper calls to `willChangeValue(forKey:)` and `didChangeValue(forKey:)`. This is useful when implementing mutator methods for transient attributes. + + - parameter value: the value to set the KVC key with + - parameter KVCKey: the KVC key + - parameter didSetValue: called after executing `setPrimitiveValue(forKey:)`. + */ + @nonobjc @inline(__always) + public func setValue(_ value: Any, forKvcKey KVCKey: KeyPath, didSetValue: () -> Void) { + + self.willChangeValue(forKey: KVCKey) + defer { + + self.didChangeValue(forKey: KVCKey) + } + self.setPrimitiveValue(value, forKey: KVCKey) + didSetValue() + } + /** Provides a convenience wrapper for setting `setPrimitiveValue(_:forKey:)` with proper calls to `willChangeValue(forKey:)` and `didChangeValue(forKey:)`. This is useful when implementing mutator methods for transient attributes. @@ -228,7 +247,7 @@ public extension NSManagedObject { self.setPrimitiveValue(value, forKey: KVCKey) } - @available(*, deprecated, renamed: "setValue(_:forKvcKey:willSetValue:didSetValue:)") + @available(*, deprecated, renamed: "setValue(_:forKvcKey:didSetValue:)") @discardableResult @nonobjc public func setValue(_ value: Any?, forKVCKey KVCKey: KeyPath, _ didSetPrimitiveValue: (Any?) throws -> T) rethrows -> T {