title: Swift 3 - Core Data Saving with one-to-many relationship
body: <p>I'm working with core data and i have two entity:</p>
  
  <p>SwimmingPool
  Parameters
  Each swimming pool can have multiple parameters</p>
  
  <p><a href="https://i.stack.imgur.com/Lz1Ic.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Lz1Ic.png" alt="relation between swimming pool and parameters"></a></p>
  
  <p><a href="https://i.stack.imgur.com/ZuabZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZuabZ.png" alt="swimming pool entity"></a></p>
  
  <p><a href="https://i.stack.imgur.com/J4MK6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J4MK6.png" alt="parameters entity"></a></p>
  
  <p>I have already saved and fetched data for swimming pool. </p>
  
  <p>I've saved them trough a form.</p>
  
  <p>In my AppDelegate i have:</p>
  
  <pre><code>let ad = UIApplication.shared.delegate as! AppDelegate
  let context = ad.persistentContainer.viewContext
  </code></pre>
  
  <p>and here's the func:</p>
  
  <pre><code> var swimmingpool: SwimminPool!
  
      if swimmingPoolToEdit == nil {
          swimming = SwimminPool(context: context)
      } else {
          swimming = swimmingPoolToEdit
      }
  
      if let name =  swimmingName.text {
          swimming.name = name
      }
      if let volume = swimmingVolume.text {
          swimming.volume = volume
  .........................................................
  ad.saveContext()
  </code></pre>
  
  <p>and i perform the fetch like so:</p>
  
  <pre><code>var controller: NSFetchedResultsController&lt;SwimmingPool&gt;!
  
  func attemptFetch() {
  
      let fetchRequest: NSFetchRequest&lt;SwimminPool&gt; = SwimminPool.fetchRequest()
      let dataSort = NSSortDescriptor(key: "createdAt", ascending: false)
  
      fetchRequest.sortDescriptors = [dataSort]
  
       let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
  
      controller.delegate = self
      self.controller = controller
  
      do {
          try controller.performFetch()
      } catch {
          let error = error as NSError
          print("\(error.debugDescription)")
      }
  }
  </code></pre>
  
  <p>Through a segue i pass all the data to a new controller and through a new form (in an another controller) i'm trying to save the data for the parameters and connect them to the the specific swimming pool.</p>
  
  <p>Do i need to save the data like i did for the swimming pool and the relationship will automatically connect the two entities or do i need to connect them upon saving?</p>
  
  <p>I'm new to swift and i'm not sure how to save related datas</p>
  
  <p>The connection will be made upon saving or when i will perform the parameters fetch?</p>
  
  <p>Thank you!</p>