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

标签 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 天,但一无所获。我希望你能帮助我。

谢谢。

最佳答案

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/

相关文章:

iOS:将 View 输入映射到 NSManagedObject 的模式

javascript - node.js mongodb - collection.find().toArray(callback) - 回调不会被调用

来自在线数据库的android可扩展列表

database - 如何在 Slick 3.1 中使用不支持的数据库

ios - 一个应用程序和两个数据库

ios - 将核心数据与服务器响应同步

php - MySQL 表中的同名字段会互相覆盖 - Laravel

ios - 带有 iOS 9.2 错误的 Xcode 7.1(找不到开发者磁盘镜像)

ios - 从 Firebase 打印数据

ios - 游戏卡在安全区域 iPhone Xs