gpt4 book ai didi

iOS9 Xcode 7 - 核心数据 - 避免重复对象

转载 作者:IT王子 更新时间:2023-10-29 05:52:04 26 4
gpt4 key购买 nike

如 WWDC2015 presentation video 中所述,在新的 Xcode7 中,我们可以直接在 Xcode 模型编辑器中设置对象的唯一性。我试图实现我的代码,但有些东西没有按预期工作。当我尝试保存重复的对象时,Xcode 拒绝保存,但表格会更新为重复的单元格。

所以我设置了独特的属性开始日期和结束日期。

enter image description here

然后我修改了我的保存函数来处理错误并通过 UIAlertController 通知用户。

func addContract() {
do {
let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = appDelegate.managedObjectContext

let entity = NSEntityDescription.entityForName("Contract", inManagedObjectContext: context)
let newContractData = Contract(entity: entity!, insertIntoManagedObjectContext: context)

newContractData.startdate = dateFormatter.dateFromString(startDateTextField.text!)!
newContractData.enddate = dateFormatter.dateFromString(endDateTextField.text!)!
newContractData.ship = shipNameTextField.text!
newContractData.position = positionOnBoardTextField.text!
newContractData.workingdays = Int(workingDaysLabel.text!)!

try context.save()
} catch {
let alertController = UIAlertController(
title: "Error",
message: "The contract exsist",
preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(
title: "OK",
style: UIAlertActionStyle.Cancel,
handler: nil)
alertController.addAction(okAction)
presentViewController(alertController, animated: true, completion: nil)
}
}

到目前为止一切顺利,但是当我使用取消按钮返回到根 Controller 时,表格似乎更新了重复的单元格。

@IBAction func cancelButtonPressed(sender: UIBarButtonItem) {
self.navigationController?.popToRootViewControllerAnimated(true)
}

此外,停止并运行应用程序会删除重复项。

这是一个 video有问题的行为。

生成的错误如下:

Error Domain=NSCocoaErrorDomain Code=1551 "The operation couldn’t be completed. (Cocoa error 1551.)" UserInfo=0x7fc02d462190 {Conflicts=(
{
constraint = (
startdate,
enddate
);
entity = Contract;
objects = (
"<Contract: 0x7fc02d45ba60> (entity: Contract; id: 0x7fc02d019430 <x-coredata:///Contract/t0897571B-200B-4F04-AF87-D50831E2DE672> ; data: {\n enddate = \"2017-06-13 21:00:00 +0000\";\n position = test;\n ship = test;\n startdate = \"2016-06-13 21:00:00 +0000\";\n workingdays = 366;\n})",
"<Contract: 0x7fc02b7433c0> (entity: Contract; id: 0xd000000000100000 <x-coredata://C3318932-BEDB-4AB6-A856-103F542BCF44/Contract/p4> ; data: {\n enddate = \"2017-06-13 21:00:00 +0000\";\n position = test;\n ship = test;\n startdate = \"2016-06-13 21:00:00 +0000\";\n workingdays = 366;\n})"
);
}
)}
2015-06-14 19:54:15.880 WorkingDays[6028:2219449] popToViewController:transition: called on <UINavigationController 0x7fc02c007e00> while an existing transition or presentation is occurring; the navigation stack will not be updated.

修正addContract()方法如下:

func addContract() {
let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = appDelegate.managedObjectContext

let entity = NSEntityDescription.entityForName("Contract", inManagedObjectContext: context)
let newContractData = Contract(entity: entity!, insertIntoManagedObjectContext: context)
do {
newContractData.startdate = dateFormatter.dateFromString(startDateTextField.text!)!
newContractData.enddate = dateFormatter.dateFromString(endDateTextField.text!)!
newContractData.ship = shipNameTextField.text!
newContractData.position = positionOnBoardTextField.text!
newContractData.workingdays = Int(workingDaysLabel.text!)!

try context.save()

} catch {
let alertController = UIAlertController(
title: "Error",
message: "The contract exsist",
preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(
title: "OK",
style: UIAlertActionStyle.Cancel,
handler: nil)
alertController.addAction(okAction)
presentViewController(alertController, animated: true, completion: nil)

context.deleteObject(newContractData)
print(error)

}
}

最佳答案

您是否使用 NSFetchedResultsController 来显示数据?

貌似只有保存才能保证唯一性。但是 Core Data 仍然允许您在执行以下操作时将对象插入到 NSManagedObjectContext 中:

let newContractData = Contract(entity: entity!, insertIntoManagedObjectContext: context)

保存时,保存操作失败,但对象仍在上下文中,因此 NSFetchedResultsController 仍会显示它。

尝试从您的捕获代码中的上下文中删除该对象:

context.deleteObject(newContractData)

关于iOS9 Xcode 7 - 核心数据 - 避免重复对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30828297/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com