我有上面的CoreData方案。我怎样才能删除其中的所有内容?有没有简单快捷的方法?
更新:
好的,我试试这个
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/