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

时间:2015-06-14 10:29:58

标签: ios xcode swift swift2 ios9

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

所以我设置了startdate和enddate的唯一属性。

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)
    }
}

到目前为止一切顺利,但当我使用取消按钮返回根控制器时,表格会显示更新的重复单元格。

@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)

    }
}

1 个答案:

答案 0 :(得分:5)

您使用NSFetchedResultsController来显示数据吗?

似乎只有在保存时才能确保唯一性。 但是,当您执行以下操作时,Core Data仍允许您将对象插入NSManagedObjectContext

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

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

尝试从catch代码中删除上下文中的对象:

context.deleteObject(newContractData)