title: Core Data One to Many Relationship not saving correctly/as expected
body: <p>I have a core data project that includes the following NSManagedObjects:</p>
  
  <p>********************** FSDJump NSManaged Object</p>
  
  <pre><code>extension FSDJump {
  
  @nonobjc public class func fetchRequest() -&gt; NSFetchRequest&lt;FSDJump&gt; {
      return NSFetchRequest&lt;FSDJump&gt;(entityName: "FSDJump")
  }
  
  @NSManaged public var starSystem: String
  @NSManaged public var starPos: NSArray
  @NSManaged public var bodyName: String
  @NSManaged public var jumpDist: Float
  @NSManaged public var fuelUsed: Float
  @NSManaged public var fuelLevel: Float
  @NSManaged public var boostUsed: Bool
  @NSManaged public var systemFaction: String
  @NSManaged public var systemAllegiance: String
  @NSManaged public var systemEconomy: String
  @NSManaged public var systemGoverment: String
  @NSManaged public var systemSecurity: String
  @NSManaged public var powers: NSArray
  @NSManaged public var powerplayState: String
  @NSManaged public var timeStamp: String
  @NSManaged public var factionState: String
  @NSManaged public var factions: NSSet
  </code></pre>
  
  <p>}</p>
  
  <p>// MARK: Generated accessors for factions
  extension FSDJump {</p>
  
  <pre><code>@objc(addFactionsObject:)
  @NSManaged public func addToFactions(_ value: Factions)
  
  @objc(removeFactionsObject:)
  @NSManaged public func removeFromFactions(_ value: Factions)
  
  @objc(addFactions:)
  @NSManaged public func addToFactions(_ values: NSSet)
  
  @objc(removeFactions:)
  @NSManaged public func removeFromFactions(_ values: NSSet)
  </code></pre>
  
  <p>}</p>
  
  <p>********************** Factions NSManaged Object</p>
  
  <pre><code>extension Factions {
  
  @nonobjc public class func fetchRequest() -&gt; NSFetchRequest&lt;Factions&gt; {
      return NSFetchRequest&lt;Factions&gt;(entityName: "Factions")
  }
  
  @NSManaged public var name: String
  @NSManaged public var allegiance: String
  @NSManaged public var factionState: String
  @NSManaged public var government: String
  @NSManaged public var influence: Float
  @NSManaged public var fsdJump: FSDJump
  </code></pre>
  
  <p>}</p>
  
  <p>with a one to many relationship defined between FSDJump and Factions (i.e. one FSDJump may have multiple factions (with the inverse relationship defined as 'fsdJump').</p>
  
  <p>I use the following code to save multiple Factions (where they exist) within an 'if else' statement saves each fsdJumpEvent.</p>
  
  <pre><code>                if fsdJumpEvent["Factions"].exists() {
  
                  // save all of the Faction array objects in the variable arrayOfFactions
                  let arrayOfFactions = fsdJumpEvent["Factions"]
  
                  // create a newFaction managedObject to save the faction details to
                  let newFaction = (Factions(context: contextTCCEDJ))
                  for faction in arrayOfFactions {
  
                      // the following is strictly not necesary but it makes the code easier to read
                      // first allocate the values from faction to a local variable then allocate that variable to the newFaction managedObject
                      // Note faction is a tuple (string, JSON) so the construct 'faction.1' accesses the second value in the tuple
                      // 'faction.0' would access the first value in the tuple which is the array index "0", "1", "2", etc
                      let newFactionName = faction.1["Name"].string!
                      let newFactionState = faction.1["FactionState"].string!
                      let newFactionGovernment = faction.1["Government"].string!
                      let newFactionAllegiance = faction.1["Allegiance"].string!
                      let newFactionInfluence = faction.1["Influence"].float!
  
                      newFaction.name = newFactionName
                      newFaction.allegiance = newFactionAllegiance
                      newFaction.government = newFactionGovernment
                      newFaction.influence = newFactionInfluence
                      newFaction.factionState = newFactionState
                      // Add the new object to the context allowing it to be saved.
                      fsdJump.addToFactions(newFaction)
                      print("Faction added \(newFaction)")
                  }
              }
  </code></pre>
  
  <p>The code appears to work.  It builds, it runs and the <code>print("Faction added \(newFaction)")</code> statement prints multiple Factions per FSDJump as expected when they exist and as per the source data file I am using (JSON).</p>
  
  <p>I can fetch the results and display them in a NSTableView. I can load data without any problems for the FSDJump managed object and display that in a NSTableView.</p>
  
  <p>The fetch code is:</p>
  
  <pre><code>    // Function returns all of the Factions associated with the timeStamp related entry in FSDJumps using the fsdJump relationship
  func eventFactionsFetchSavedDataFromPersistenStore (contextTCCEDJ: NSManagedObjectContext, timeStamp: String) -&gt; [Factions] {
      var result = Array&lt;Factions&gt;()
      let localFetchRequest = Factions.fetchRequest() as NSFetchRequest
      localFetchRequest.predicate = NSPredicate(format:"fsdJump.timeStamp == '\(timeStamp)'")
      do {
          result = try contextTCCEDJ.fetch(localFetchRequest)
      } catch {
          print("Error in returning Factions event saved data from the persistent store")
      }
     print(result.count)
      for reSult in result {
      print(reSult.fsdJump.timeStamp)
      }
      return result
  }
  </code></pre>
  
  <p><strong><em>However</em></strong>, it only seems to save the last 'faction' for each relationship - rather than the multiple factions that are shown in the <code>for faction in arrayOfFactions</code> loop.  Because the fetch statement only returns one Faction per fetch.  Even if I remove the predicate statement it returns all of the saved factions but again only one has been saved per fsdJumpEvent instead of the multiple factions that are identified by the print statement.</p>
  
  <p>I have tried everything I can think of.  I have not been able to find a previous question that relates to this specific issue.</p>
  
  <p>Am I using the 'addToFactions' function incorrectly?</p>
  
  <p>Any help would be gratefully received.</p>
  
  <p>(And yes - for any Elite Dangerous fans out there I am writing a macOS app to parse my journal files as a companion app to my windows version of Elite Dangerous.)</p>