清除内存中的存储类型

时间:2019-01-21 11:55:08

标签: swift core-data nsmanagedobjectcontext

我想从Core Data中删除每个以前保存的对象。我使用的是“内存中”存储类型,因此NSBatchDeleteRequest不是一个选择。

我尝试在上下文中调用reset(),但是似乎没有任何作用。

我有以下代码:

viewContext.reset()
let fetchRequest: NSFetchRequest<CompactJobManaged> = CompactJobManaged.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "highlightedAt", ascending: false),
                                NSSortDescriptor(key: "uploadedAt", ascending: true)]
print(try! viewContext.fetch(fetchRequest).count)

我希望底部的print语句在调用reset()之后会打印出0,但是,尽管以前调用了reset(),它也会打印出先前已保存的对象数。

我可能为此目的滥用了reset()函数,但是我不知道有什么其他方法可以从内存中批量删除所有对象。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您尝试过这样的事情吗?

do {
    try persistentStoreCoordinator.destroyPersistentStoreAtURL(persistentStoreURL, withType: NSSQLiteStoreType, options: nil)

} catch {
    // Error Handling
}