Search code examples
macoscore-datanstableviewnsarraycontroller

macOS Core Data – are toOne new records automatically assigned to the toMany Object the moment they are created?


I have two core data entities: Category and Music.

Category has a toMany relationship. Music has a toOne relationship.

Each entity display their values on separate tables. Tables are populated by independent NSArrayControllers.

I select a category on its table.

I create a new Music by this line on the code on MusicArrayController:

let newAttribute = self.newObject() as! Music
newAttribute.label = "New Music"
self.addObject(newAttribute)

If after that, that I check the last category I have selected, I see this new object is already in the category's toMany NSSet.

Example:

  1. I select Rock and Roll in the category table.
  2. Table 2 show the musics under that category.
  3. I add a new music.
  4. The Rock and Roll category has already that new music listed on its toMany NSSet.

I have not added any code to do that assignment.

Is this Core Data normal behavior?


Solution

  • The array controller shows and manages the Music objects of the Category. When a Music object is added with self.addObject(newAttribute) then the array controller adds the Music object to the relationship. This is normal NSArrayController behaviour, with and without Core Data.