Skip to content

Instantly share code, notes, and snippets.

@arpitdsoni
Created March 25, 2020 16:46
Show Gist options
  • Save arpitdsoni/8f1e588f9acbe8df6cdaa5aba797f36b to your computer and use it in GitHub Desktop.
Save arpitdsoni/8f1e588f9acbe8df6cdaa5aba797f36b to your computer and use it in GitHub Desktop.
protocol SafeValueForKeyPath {}
extension SafeValueForKeyPath where Self: CFXManagedObject {
func safeValueForKeyPath<Value>(_ keyPath: KeyPath<Self, Value?>) -> Value? {
let keyPathStr = NSExpression(forKeyPath: keyPath).keyPath
return managedObjectContext?.performAndWait {
return value(forKeyPath: keyPathStr) as? Value
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment