Swift - 从数据库中删除单个记录

时间:2015-04-21 07:21:01

标签: database swift core-data

我正在尝试从数据库中删除单个记录。

if let items=fetchedResult{
            print(items.count) //this always prints "1", so it's working well
            for item:NSManagedObject in items{
                println(item) //this prints the only element found. Also working well
                println(sender.view!.restorationIdentifier!) //the restorationIdentifier matches with the id from the object in the database. Also working well
                managagedContext.deleteObject(item) //<-- THIS is what is not working
                sender.view?.removeFromSuperview() //removing the view well
            }
        }else{
            print("Could not delete \(error), \(error!.userInfo)")
        }

如评论中所述,行managagedContext.deleteObject(item)无效,我没有收到任何错误,只是没有删除数据库中的对象。

我一直在寻找2天而一无所获。我希望你能帮助我。

感谢。

1 个答案:

答案 0 :(得分:2)

deleteObject

Apple's doc

  

指定在提交更改时应从其持久性存储中删除的对象

您需要致电context.save以使更改保持不变。 save documentation says that:

  

如果上下文的父存储是持久存储协调器,则更改将提交到外部存储

另外:this SO answer显示调用save:和管理NSError

的详细信息