我正在尝试从数据库中删除一条记录。
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 天,但一无所获。我希望你能帮助我。
谢谢。
最佳答案
Apple's doc对于 deleteObject
Specifies an object that should be removed from its persistent store when changes are committed.
您需要调用context.save
来使您的更改持久化。 save documentation says that:
If a context’s parent store is a persistent store coordinator, then changes are committed to the external store
还有:this SO answer显示调用 save: 和管理 NSError 的详细信息
关于database - Swift - 从数据库中删除单条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29765491/