'CoreData child objects not stored in a Parent Child relationship

In order to demonstrate my question I have prepared a minimal example in which a data model is defined in CoreData as follows:

Parent: enter image description here

Child: enter image description here

The problem which I encountered is that the data of the child objects are not written to the store permanently. When I restart the app, the data of the child objects do not appear.

Here the code with which I add objects to the context and save it. For the purpose of this test, two child objects are created and attached to the parent object.

   @IBAction func addRecord(_ sender: Any) {
    let alert = UIAlertController(title: "New parent",
                                  message: "Add a new name",
                                  preferredStyle: .alert)
    
    alert.addTextField()
    
    let submitButton = UIAlertAction(title: "Add", style: .default) { (action) in
        
        // Get the textfield from the alert
        let textfield = alert.textFields![0]
        
        // Create a parent object
        let newParent = Parent(context: self.context)
        newParent.name = textfield.text
        
        // Two child objects shall be added
        let child_1 = Child(context: self.context)
        child_1.name = "Lea"
        let child_2 = Child(context: self.context)
        child_2.name = "Nora"

        // even if I activate the following save operation
        // it does not make a difference:
        /*
        do {
            try self.context.save()
        }
        catch {
            print("error with self.context.save()")
        }
         */
     
        var allChildren = newParent.children?.allObjects
        allChildren?.append(child_1)
        allChildren?.append(child_2)
        newParent.children = NSSet(array: allChildren!)
                   
        // Save the data
        do {
            try self.context.save()
        }
        catch {
            print("error with self.context.save()")
        }
        
        //Re-fetch the data
        self.fetchParents()
    }
    
    // Add button
    alert.addAction(submitButton)
    
    // Show Alert
    self.present(alert, animated: true, completion: nil)
}

As you see at the end, self.fetchParents is executed after the save. And when the data are loaded from the store at this point, the child records (p7, p8) are existing as this example shows from my console:

  • 5 : <Parent: 0x600003906b20> (entity: Parent; id: 0x97aadc54db61ef54 x-coredata://7606C81E-A3B7-4D7A-838E-0E3B01913B2D/Parent/p6; data: { children = ( "0x97aadc54daa1ef44 x-coredata://7606C81E-A3B7-4D7A-838E-0E3B01913B2D/Child/p8", "0x97aadc54db41ef44 x-coredata://7606C81E-A3B7-4D7A-838E-0E3B01913B2D/Child/p7" ); name = NameOfANewParent;

But when the app is restarted, the child objects are missing:

- 5 : <Parent: 0x600003b7d1d0> (entity: Parent; id: 0x91bfb477f1017183 <x-coredata://7606C81E-A3B7-4D7A-838E-0E3B01913B2D/Parent/p6>; data: {
children = "<relationship fault: 0x600001870d20 'children'>";
name = NameOfANewParent;

So, I must be missing an essential step. What is it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source