ios - 删除所有核心数据数据

标签 ios swift core-data

我有上面的CoreData方案。我怎样才能删除其中的所有内容?有没有简单快捷的方法?

enter image description here

更新:

好的,我试试这个

func flushDatabase() {
    if let moc = self.managedObjectContext{
        moc.lock()

        if let stores = persistentStoreCoordinator?.persistentStores as? [NSPersistentStore] {
            for store in stores {
                persistentStoreCoordinator?.removePersistentStore(store, error: nil)
                if let path = store.URL?.path {
                    NSFileManager.defaultManager().removeItemAtPath(path, error: nil)
                }
            }
        }

        moc.unlock()



    }
}

但是我如何重新创建 persistenStoreCoordinator 呢?

更新 2

 class func deleteAllMinigames(context:NSManagedObjectContext) {
        context.lock()
        let fetchRequest = NSFetchRequest(entityName: "Minigame")
//        fetchRequest.includesSubentities = true
//        fetchRequest.includesPropertyValues = false
        var error: NSError?
        if let items = context.executeFetchRequest(fetchRequest, error: &error) as? [Minigame]{
            for item in items {
                if item.validateForDelete(&error){
                    context.deleteObject(item)
                }else{
                    println(error?.localizedDescription)
                }

            }
        }
        context.unlock()

    }

当我试图删除所有迷你游戏时,我得到了这个。我试图将所有删除规则添加到级联中,但它不起作用

*Optional("The operation couldn’t be completed. (Cocoa error 1560.)")
Optional("The operation couldn’t be completed. (Cocoa error 1560.)")
Optional("The operation couldn’t be completed. (Cocoa error 1560.)")
Optional("The operation couldn’t be completed. (Cocoa error 1560.)")
Optional("The operation couldn’t be completed. (Cocoa error 1560.)")
Optional("The operation couldn’t be completed. (Cocoa error 1560.)")*

最佳答案

这意味着有一个强制属性已被分配为零。在您的 *.xcodatamodel 中选中“可选”框,或者在保存到 managedObjectContext 时确保您的属性已填写。

如果您在更改代码以满足这两个要求后出现更多错误,请尝试清理您的构建并从您的 iPhone 模拟器/iPhone 设备中删除该应用程序。您的模型更改可能与旧模型实现冲突。

编辑:

我差点忘了这是 Core Data 吐出的所有错误代码:Core Data Constants Reference我以前遇到过这个问题,我意识到我取消选中了正确的可选框。这样的麻烦找出问题所在。祝你好运。

关于ios - 删除所有核心数据数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29841077/

相关文章:

ios - 为什么 EXIF GPS 坐标在 iOS 上被截断?

ios - Apple Pay token 的交易金额不正确

ios - 从 firebase 检索子值到数组以在 TableView 单元格中使用

iphone - 使用 Objective C 根据经纬度搜索周边郊区

ios - coredata 中的继承属性与继承模型发生冲突

iphone - 旋转后将文本居中对齐

ios - Xcode 6 Swift - 显示本地 PDF

ios - ActionSheet 消失后执行动画

ios - UILocalNotification 多次触发 swift

ios - 在我的 iOS 项目中是否使用文件或使用核心数据的 sqlite 数据库?