【发布时间】:2023-03-06 16:11:01
【问题描述】:
当使用带有loadPersistentStores 的“新”iOS 10 Core Data 设置时,如何重置/删除所有内容?我想避免使用实体名称,但喜欢使用 destroyPersistentStore 之类的名称。
我的堆栈设置如下:
persistentContainer = NSPersistentContainer(name: "CoreData", managedObjectModel: mom)
let storeDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let url = storeDirectory.appendingPathComponent("CoreData.sqlite")
let description = NSPersistentStoreDescription(url: url)
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true
persistentContainer.persistentStoreDescriptions = [description]
persistentContainer.loadPersistentStores(completionHandler: { (_, error) in
guard let error = error as NSError? else { return }
fatalError("Unresolved error: \(error), \(error.userInfo)")
})
persistentContainer.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
persistentContainer.viewContext.undoManager = nil
persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
我只找到了解决方案,例如BatchDeleteRequests 我需要知道所有实体名称,但我想要更通用的东西并重置所有内容 - 当用户注销时,我需要最高效/最安全的方式。
【问题讨论】:
-
destroyPersistentStoreAtURL(_:withType:options:) 有帮助吗?
-
之后如何重新设置?
-
但我不是第一次这样做,那么完整的东西应该是什么样子?不知道这是否是唯一/最好的方法......