我需要清除CoreData中的所有Core数据实体数据,而不是一个实体。即不应删除一个实体。一种方法是逐个实体清除实体,并为所需的皮肤设置外观。但是我们可以将实体备份还是保留在RAM中,清除所有Db并重新保存实体。有可能吗?
答案 0 :(得分:0)
您可以使用NSBatchDeleteRequest
批量删除实体,代码假定managedObjectContext
为当前管理对象上下文:
let entityNamesToDelete = ["Foo", "Bar", "Baz"]
let persistentStoreCoordinator = managedObjectContext.persistentStoreCoordinator!
do {
for entityName in entityNamesToDelete {
let request = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
let deleteRequest = NSBatchDeleteRequest(fetchRequest: request)
try persistentStoreCoordinator.execute(deleteRequest, with: managedObjectContext)
}
try managedObjectContext.save()
} catch { print(error) }