• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
How to validate Core Data attributes ? (example in official doc doesn't work)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: How to validate Core Data attributes ? (example in official doc doesn't work)
      • From: Jerome Krinock <email@hidden>
    • Re: How to validate Core Data attributes ? (example in official doc doesn't work)
      • From: Quincey Morris <email@hidden>
  • Prev by Date: Re: iOS Drag and Drop
  • Next by Date: Re: How to validate Core Data attributes ? (example in official doc doesn't work)
  • Previous by thread: Re: iOS Drag and Drop
  • Next by thread: Re: How to validate Core Data attributes ? (example in official doc doesn't work)
  • Index(es):
    • Date
    • Thread