title: Swift - How to save entries to core data in a loop
body: <p>I have 2 entities: Series and Season. A Series can have multiple seasons, so I set the relationship type to "to many". season_counter is an Int Array, containing the amount of episodes.</p>
  
  <pre><code>let newSeries = NSEntityDescription.insertNewObjectForEntityForName("Series", inManagedObjectContext: self.context!) as! Series
  
  for var i = 0; i &lt; season_counter.count; ++i{
           let newSeason = NSEntityDescription.insertNewObjectForEntityForName("Season", inManagedObjectContext: self.context!) as! Season
           newSeason.value = i+1
           newSeason.episodes = season_counter[i]
           newSeries.setValue(NSSet(object: newSeason), forKey: "seasons")
  
       do {
           try context?.save()
      } catch _ {
      }
  }
  </code></pre>
  
  <p>While debugging I noticed, that season_counter stores the correct values. When I display the results, I have only the last season stored (for example 13 episodes, seasons.count is 1):</p>
  
  <pre><code>do {
      let fetchRequest = NSFetchRequest(entityName: "Season")
      fetchRequest.predicate = NSPredicate(format: "series = %@", series)
      try seasons = context?.executeFetchRequest(fetchRequest) as! [Season]
  
      print(seasons.count)
      print(seasons[0].episodes)
  } catch {
  }
  </code></pre>
  
  <p>Any tips to solve this?</p>