title: CoreData and Swift: contents gets swapped
body: <p>I am new to swift and have a problem with CoreData: I can save and load to and from my CoreData store. But when I load the saved data from CoreData the data has been rearranged. I don't want this to happen. How do I solve this?</p>
  
  <p>My code is this:</p>
  
  <p>Meetings.swift</p>
  
  <pre><code>import Foundation
  import CoreData
  
  class Meetings: NSManagedObject {
      @NSManaged var meetingTimeLeft: NSNumber
      @NSManaged var title: String
      @NSManaged var date: NSDate
      @NSManaged var timeFrom: NSDate
      @NSManaged var timeTo: NSDate
      @NSManaged var timeFromActual: NSDate
      @NSManaged var timeToActual: NSDate
      @NSManaged var location: String
      @NSManaged var locationRoom: String
      @NSManaged var meetingGoals: String
      @NSManaged var purpose: String
      @NSManaged var averageHourlyCost: NSNumber
      @NSManaged var completed: NSNumber
      @NSManaged var durationActual: NSNumber
      @NSManaged var durationPlanned: NSNumber
      @NSManaged var persons: NSSet
      @NSManaged var agendas: NSSet
      @NSManaged var minutes: NSSet
      @NSManaged var relationship: NSManagedObject
      @NSManaged var startSetting: StartSetting?
  
  }
  extension Meetings {
  func addPer(value: Person) {
      self.mutableSetValueForKey("persons").addObject(value)
  }
  func removePer(value: Person) {
      self.mutableSetValueForKey("persons").removeObject(Person)
  
      //self.mutableSetValueForKey("persons").addObject(value)
  }
  
  
  func getPer() -&gt; [Person] {
      var persons: [Person]
      persons = self.persons.allObjects as! [Person]
      return persons
  }
  func addAgenda(value: AgendaItem) {
      self.mutableSetValueForKey("agendas").addObject(value)
  }
  func removeAgenda(value: AgendaItem) {
      self.mutableSetValueForKey("agendas").removeObject(AgendaItem)
  
      //self.mutableSetValueForKey("persons").addObject(value)
  }
  
  
  func getAgenda() -&gt; [AgendaItem] {
      var agendas: [AgendaItem]
      agendas = self.agendas.allObjects as! [AgendaItem]
      return agendas
  }
  func getAgendaItem(i: Int) -&gt; AgendaItem {
      var agendas: [AgendaItem]
      agendas = self.agendas.allObjects as! [AgendaItem]
      let agendaItem = agendas[i]
      return agendaItem
  }
  }
  </code></pre>
  
  <p>person.swift</p>
  
  <pre><code>import Foundation
  import CoreData
  
  class Person: NSManagedObject {
  
      @NSManaged var name: String
      @NSManaged var email: String
      @NSManaged var phone: NSNumber
      @NSManaged var title: String
      @NSManaged var company: String
      @NSManaged var photo: NSData
      @NSManaged var meeting: Meetings
  }
  </code></pre>
  
  <p>AgendaItem.swift</p>
  
  <pre><code>import Foundation
  import CoreData
  
  class AgendaItem: NSManagedObject {
      @NSManaged var agendaItemTitle: String
      @NSManaged var presenter: String
      @NSManaged var durationPlanned: NSNumber
      @NSManaged var durationActual: NSNumber
      @NSManaged var filePresentation: NSData
      @NSManaged var fileSupporting: NSData
      @NSManaged var agendaTimeStart: NSDate
      @NSManaged var meeting: Meetings
  }
  </code></pre>
  
  <p>when saving i do like this:</p>
  
  <pre><code>internal var meetingsModels = [Meetings]()
  public let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
  
  managedObjectContext?.deleteObject(meetingsModels[meetingsIndexPassed.row]) meetingsModels.removeAtIndex(meetingsIndexPassed.row)
  let entityDescription = NSEntityDescription.entityForName("Meetings", inManagedObjectContext: managedObjectContext!)
  let entityDescription2 = NSEntityDescription.entityForName("Person", inManagedObjectContext: managedObjectContext!)
  let entityDescription3 = NSEntityDescription.entityForName("AgendaItem", inManagedObjectContext: managedObjectContext!)
  
  
  let meetings = Meetings(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext)
  
  meetings.title = meetingTitle_txt.text!
  meetings.date = lDate
  meetings.timeFrom = lStartTime
  meetings.timeTo = lEndTime
  meetings.location = address_txt.text!
  meetings.purpose = details[0]
  meetings.meetingGoals = details[1]
  meetings.locationRoom = details[2]
  meetings.durationPlanned = meetingsEditPassed.durationPlanned
  meetings.completed = false
  
  for var i = 0; i &lt; personsEdit.count; i++ {
      let person = Person(entity: entityDescription2!, insertIntoManagedObjectContext: managedObjectContext)
      person.name = personsEdit[i].name
      person.email = personsEdit[i].email
      person.title = personsEdit[i].title
      person.company = personsEdit[i].company
      person.phone = personsEdit[i].phone
      meetings.addPer(person)
  }
  for var i = 0; i &lt; agendaEdit.count; i++ {
      let agenda = AgendaItem(entity: entityDescription3!, insertIntoManagedObjectContext: managedObjectContext)
      agenda.agendaItemTitle = agendaEdit[i].agendaItemTitle
      //print(agendaEdit[i].agendaItemTitle)
      agenda.presenter = agendaEdit[i].presenter
      agenda.durationPlanned = agendaEdit[i].durationPlanned
      meetings.addAgenda(agenda)
  }
  
  do {
      try managedObjectContext?.save()
  } catch let error1 as NSError {
      print("\(error1)")
  }
  meetingsModels.append(meetings)
  </code></pre>
  
  <p>when loading i do this:</p>
  
  <pre><code>override func viewWillAppear(animated: Bool) {
      super.viewWillAppear(animated)
      //1
      let appDelegate =
      UIApplication.sharedApplication().delegate as! AppDelegate
  
      let managedContext = appDelegate.managedObjectContext!
  
      //2
      let fetchRequest = NSFetchRequest(entityName:"Meetings")
      //fetchRequest.sortDescriptors = nil
      //3
      let fetchedResults: [Meetings]
      do {
          try
          fetchedResults = (managedContext.executeFetchRequest(fetchRequest) as? [Meetings])!
          if let results: [Meetings] = fetchedResults {
              var temp = [Meetings]()
              var temp2 = [Meetings]()
              for tmp: Meetings in results {
                  temp.append(tmp)
                  if tmp.completed.boolValue {
                      temp2.append(tmp)
                  }else {
  
                  }
              }
              meetingsModels = temp
              meetingsModels2 = temp2
          } else {
          }
      } catch let error1 as NSError {
          print("could not fetch meetings: \(error1), \(error1.userInfo)")
      }
  
  
  
  }
  </code></pre>
  
  <p>i hope that some of you can help me.</p>