第一次成功保存上一个对象后,第二次尝试将新对象保存到Core Data时遇到问题。
这两个相关实体是儿童和护理:
class Child:NSManagedObject {
@NSManaged var avatar:NSData!
@NSManaged var birthdate:NSDate!
@NSManaged var datecreated:NSDate!
@NSManaged var datemodified:NSDate!
@NSManaged var bloodtype:NSString!
@NSManaged var eyecolor:String
@NSManaged var firstname:String!
@NSManaged var gender:String!
@NSManaged var haircolor:String!
@NSManaged var baths:NSSet!
}
class Nursing : NSManagedObject {
@NSManaged var leftduration: Double
@NSManaged var leftstarttime: NSDate?
@NSManaged var mood: String
@NSManaged var note: String
@NSManaged var rightduration: Double
@NSManaged var rightstarttime: NSDate?
@NSManaged var spitup: Bool
@NSManaged var child: Child
@NSManaged var completed: Bool
@NSManaged var leftstate: String
@NSManaged var rightstate: String
}
儿童与护理之间存在一对多的关系: 儿童< - >>护理
以下是创建新记录并将其保存到Core Data的功能:
func createNewNursingRecordWithSide(side: String) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var nursing: AnyObject! = NSEntityDescription.insertNewObjectForEntityForName("Nursing", inManagedObjectContext: managedObjectContext!)
//println("currentChild: \(currentChild?.description)")
nursing.setValue(currentChild!, forKey: "child")
if side == "left" {
nursing.setValue(NSDate(), forKey: "leftstarttime")
nursing.setValue("active", forKey: "leftstate")
}else{
nursing.setValue(NSDate(), forKey: "rightstarttime")
nursing.setValue("active", forKey: "rightstate")
}
println("nursing: \(nursing.description)")
var error:NSError?
if managedObjectContext!.save(&error) != true {
println("insert error: \(error!)")
return
}else{
activeNursing = (nursing as! Nursing)
}
}
当我第一次成功保存记录时调用此函数,但如果我第二次调用它,则会出现以下错误:
insert error: Error Domain=NSCocoaErrorDomain Code=1570 "The operation couldn’t be completed. (Cocoa error 1570.)" UserInfo=0x17026e940 {NSValidationErrorKey=child, NSLocalizedDescription=The operation couldn’t be completed. (Cocoa error 1570.), NSValidationErrorObject=<Baby_Master.Nursing: 0x1740ce310> (entity: Nursing; id: 0xd000000000080002 <x-coredata://29485885-9E6E-4BCA-8B4C-1617F191B7BC/Nursing/p2> ; data: {
child = nil;
completed = 1;
leftduration = 0;
leftstarttime = "2015-06-23 15:51:27 +0000";
leftstate = ready;
mood = nil;
note = nil;
rightduration = 0;
rightstarttime = "2015-06-23 15:51:03 +0000";
rightstate = ready;
spitup = 0;
})}
奇怪的是,数据是试图保存不是我传递给新的“护理”变量,而是先前保存的记录数据。
当我打印新的护理变量数据时,我得到了这个:
nursing: <Baby_Master.Nursing: 0x1740cdeb0> (entity: Nursing; id: 0x174226560 <x-coredata:///Nursing/t3C52AE5B-96E0-48F4-BF92-6185B00E2CC12> ; data: {
child = "0xd000000000080000 <x-coredata://29485885-9E6E-4BCA-8B4C-1617F191B7BC/Child/p2>";
completed = 0;
leftduration = 0;
leftstarttime = nil;
leftstate = ready;
mood = nil;
note = nil;
rightduration = 0;
rightstarttime = "2015-06-23 17:11:54 +0000";
rightstate = active;
spitup = 0;
})
您可以看到我正在尝试保存的“护理”变量包含对“子”数据的引用,这不是可选的,这也是我收到错误的原因。
我的问题是,为什么新的“护理”变量不是使用新数据而不是旧数据?
感谢您的帮助!
答案 0 :(得分:0)
当然,问题归结为我的一个简单疏忽。事实证明我错过了@NSManaged var nursings:NSSet!在我的儿童模特课中。
咄!
固定模型是:
class Child:NSManagedObject {
@NSManaged var avatar:NSData!
@NSManaged var birthdate:NSDate!
@NSManaged var datecreated:NSDate!
@NSManaged var datemodified:NSDate!
@NSManaged var bloodtype:NSString!
@NSManaged var eyecolor:String
@NSManaged var firstname:String!
@NSManaged var gender:String!
@NSManaged var haircolor:String!
@NSManaged var baths:NSSet!
@NSManaged var nursings: NSSet!
@NSManaged var diapers: NSSet!
}
希望这有助于其他遇到此问题的人。