How to validate Core Data attributes ? (example in official doc doesn't work)
How to validate Core Data attributes ? (example in official doc doesn't work)
- Subject: How to validate Core Data attributes ? (example in official doc doesn't work)
- From: Glen Huang <email@hidden>
- Date: Wed, 14 Jun 2017 10:10:15 +0800
Hi,
I'm trying to validate an attribute of a NSManagedObject, I create a
transformable attribute called location, make it CLLocation, and define a
method on the object:
func validateLocation(_ value:
AutoreleasingUnsafeMutablePointer<AnyObject?>) throws {
if value.pointee == nil {
fatalError("Location can't be nil")
}
}
Which is adapted from the official document. In the Core Data Xcode editor, I
enabled optional for this attribute, but I expect this method will prevent the
object from being saved when the it's location isn't set and the context is
saved.
But when I do that, the method isn't even called. I can confirm by setting a
break point in the method, and it never blocks.
Apart from the previous method, I add the following to func application(_:,
didFinishLaunchingWithOptions launchOptions:)
let context = persistentContainer.viewContext
let foo = Foo(context: context)
foo.name = "foo"
// The following should fail, since we didn't provide location value
try! context.save()
print("succeed")
Where Foo has a name attribute of type string and the location attribute as I
mentioned. In my case "succeed" prints in the console.
Did I do something wrong or is it a bug on the Core Data side?
I'm using Xcode 9 beta on macOS 10.12.5, with Swift 4 and the new building
system.
Thanks
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden