0

I merely want to add an entity to an existing NSManagedObject Data Graph and access that entity in code. In this case, I've added 'Address':

enter image description here

The existing code works. I merely added:

let address = Address(context:self.context!)

...as was originally done with the Person() entity.

Here's the error:
enter image description here

Here's the code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions
        launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

        self.context = persistentContainer.viewContext
        let storyboard = self.window?.rootViewController?.storyboard
        guard let vc = storyboard?.instantiateViewController(withIdentifier: "RootViewController") as? RootViewController
            else { fatalError("Cannot instantiate root view controller") }
        vc.context = self.context
        self.window?.rootViewController = vc    // Note: necessary to keep the app del's context instance within vc.

        let address = Address(context:self.context!)

        let person = Person(context: self.context!)
        person.firstName = "Foo"
        person.lastName = "Bar"

        person.cars = NSSet(array: [
            Car(context: self.context!).configured(maker: "VW",
                                                   model: "Sharan",
                                                   owner: person),
            Car(context: self.context!).configured(maker: "VW",
                                                   model: "Tiguan",
                                                   owner: person)
            ])

        do{
            try saveContext()
        } catch {
            //something bad happened, handle this situation appropriately
        }

        return true
    }

Question: How do I make the code see the new entity 'Address'?

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
  • What property is set for codegen on the entity? Did you generate subclasses? – Jon Rose Feb 27 '17 at 07:50
  • No. I'm a neophyte here. Just working with some working code and playing with the object graph. – Frederick C. Lee Feb 27 '17 at 17:58
  • Since Xcode 7 or 8, there's problem with the automatic saving of Data Models. Make the the data model active in the editor and do cmd-s a few times. Use the File menu to close the Data Model file, then reopen it to see if the changes are still there. Clean and build. Dance around the computer a few times. Eventually, Core Data will get the message. – Elise van Looij Mar 05 '17 at 22:39

0 Answers0