title: Adding new object to CoreData with one to many relation
body: <p>I have CoreData model like this:</p>
  
  <p><a href="https://i.stack.imgur.com/UhLeT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UhLeT.png" alt="enter image description here"></a></p>
  
  <p>Parcel can have only one company, but company can have multiple parcels to deliver.</p>
  
  <p>I have three companies in database preloaded. I have created table view with sections and loading data via NSFetchedResultsController.
  I'm configuring it like this:</p>
  
  <pre><code>let fetchRequest = NSFetchRequest(entityName: EnityNames.PackageInfoEnityName)
  
          // Add Sort Descriptors
          let sortDescriptor = NSSortDescriptor(key: PackageInfoKeyPaths.Company, ascending: true)
          fetchRequest.sortDescriptors = [sortDescriptor]
  
          // Initialize Fetched Results Controller
          let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: PackageInfoKeyPaths.Company, cacheName: nil)
  
          // Configure Fetched Results Controller
          fetchedResultsController.delegate = self
  
          return fetchedResultsController
  </code></pre>
  
  <p>When I'm launching app on simulator I have three sections(as expected), i want to have sections as companies displayed, so I'm using relationship as section name key path:</p>
  
  <pre><code>number of sections 3
  FedEx
  UPS
  DHL
  </code></pre>
  
  <p>I have created popover where I can add new entries to database to populate list with additional data. This is the code I'm using to create and save new data:</p>
  
  <pre><code>let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
          let package = 
  
      NSEntityDescription.insertNewObjectForEntityForName(EnityNames.PackageInfoEnityName, inManagedObjectContext: managedObjectContext) as! PackageInfo
              let formatter = NSNumberFormatter()
              formatter.numberStyle = .DecimalStyle
              package.parcelNumber = formatter.numberFromString(parcelNumberTextField.text!) ?? 0;
              package.createdAt = NSDate()
              let company = avaialbleCompanies![companyPickerView.selectedRowInComponent(0)]
              package.company_relation = company
              company.addPackageToCompany(package)    
  
  
              appDelegate.saveContext()
  </code></pre>
  
  <p>the companies array is passed in prepare for segue to my little popover to let user choose only companies that are inside database. After this I'm getting something strange:</p>
  
  <pre><code>number of sections 4
  FedEx
  UPS
  UPS
  </code></pre>
  
  <p>Why it is adding new Section? It should just add new item to existing section!</p>
  
  <p>and terrifying error:</p>
  
  <p>PackageChecker[5336:281349] CoreData: error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (4) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). with userInfo (null)</p>
  
  <p>Important part can be that I'm getting this:</p>
  
  <pre><code>updated index path 1x0
  </code></pre>
  
  <p>and I'm using <code>fetchedResultsController.sections?.count</code> to get number of sections. 
  If I restart the app on sim my newly added record is present in proper section inside list. Why it is not updating properly on runtime?</p>
  
  <p>P.S. I have:</p>
  
  <pre><code>func controllerWillChangeContent(controller: NSFetchedResultsController) {
          self.tableView.beginUpdates()
      }
  
      func controllerDidChangeContent(controller: NSFetchedResultsController) {
          self.tableView.endUpdates()
      }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>If I remove sectionNameKeyPath: PackageInfoKeyPaths.Company - adding works perfectly. Can You help me with this sections? Maybe I'm configuring them poorly.</p>