我是swift的新手并且在使用CoreData时遇到问题:我可以保存并加载到我的CoreData存储中。但是当我从CoreData加载保存的数据时,数据已经重新排列。我不希望这种情况发生。我该如何解决这个问题?
我的代码是:
Meetings.swift
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() -> [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() -> [AgendaItem] {
var agendas: [AgendaItem]
agendas = self.agendas.allObjects as! [AgendaItem]
return agendas
}
func getAgendaItem(i: Int) -> AgendaItem {
var agendas: [AgendaItem]
agendas = self.agendas.allObjects as! [AgendaItem]
let agendaItem = agendas[i]
return agendaItem
}
}
person.swift
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
}
AgendaItem.swift
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
}
保存时我喜欢这样:
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 < 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 < 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)
加载时我这样做:
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)")
}
}
我希望你们中的一些人可以帮助我。
答案 0 :(得分:0)
您的会议数据正在重新排列&#34;仅仅因为您的Meetings
不属于任何有序关系。我无法在你身边运行你的代码。但是,我可以推荐您以下之一:
sortDescriptors
财产。当你
创建一个,您可以为其添加sort descriptor
安排数据。