title: One to many relationship CoreData Swift
body: <p>Core Data works great for the most part. When I click on name first VC (Items) and performSeque to the second VC (Costs), I can see the costsName and other data. But when I add second name in first VC I can see the same data as in first name.</p>
  
  <p>I'm trying to make a one to many relationship.</p>
  
  <p>I have 2 data models: </p>
  
  <pre><code>import Foundation
  import CoreData
  
  @objc(Items)
  class Items: NSManagedObject {
  
     @NSManaged var count: NSNumber
     @NSManaged var name: String
     @NSManaged var cost: NSSet
  
  }
  
  import Foundation
  import CoreData
  
  @objc(Costs)
  class Costs: NSManagedObject {
  
    @NSManaged var costsDate: NSDate
    @NSManaged var costsName: String
    @NSManaged var costsValue: NSNumber
    @NSManaged var account: Items
  
  }
  </code></pre>
  
  <p>Here is <code>addAccount</code>'s (name of the first VC) save action:</p>
  
  <pre><code>@IBAction func saveButtonPressed(sender: UIBarButtonItem) {
  
      let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
      var managedObjectContext = appDelegate.managedObjectContext
      let entityDescription = NSEntityDescription.entityForName("Items", inManagedObjectContext: managedObjectContext!)
  
      let account = Items(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext!)
  
      account.name = cellOneNameTextField.text
      if cellTwoCountTextField.text.isEmpty {
  
      } else {
  
      account.count = (cellTwoCountTextField.text).toInt()!
      }
      // Saving data
      appDelegate.saveContext()
  
      var request = NSFetchRequest(entityName: "Items")
      var error:NSError? = nil
  
      var results:NSArray = managedObjectContext!.executeFetchRequest(request, error: &amp;error)!
  
      self.navigationController?.popViewControllerAnimated(true)
  
  }
  </code></pre>
  
  <p>Here is <code>addCost</code>'s save action:</p>
  
  <pre><code>    @IBAction func saveButtonTapped(sender: UIBarButtonItem) {
  
      // CoreData Access
      let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
      var managedObjectContext = appDelegate.managedObjectContext
      let entityDescription = NSEntityDescription.entityForName("Costs", inManagedObjectContext: managedObjectContext!)
  
      let cost = Costs(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext!)
  
      cost.costsName = cellThreeNoteTextField.text
      cost.costsValue = (cellOnePriceTextField.text).toInt()!
      cost.costsDate = datePicker.date
  
      // Saving data
      appDelegate.saveContext()
  
      var request = NSFetchRequest(entityName: "Costs")
      var error:NSError? = nil
  
      var results:NSArray = managedObjectContext!.executeFetchRequest(request, error: &amp;error)!
  
      for res in results {
          println(res)
  
      }
      delegate?.refreshTable()
      self.navigationController?.popViewControllerAnimated(true)
  
  }
  </code></pre>