I have a program that reads data from 3 files in the downloads directory, places the data into managed objects and then saves them via the managed object context to persistent stores in 3 seperate containers. When the program starts it compares a date in a CreationDateContainer in persistent stores with the creation date of one of the data files in the downloads folder. If the files creation date is later than that in stores I want to delete the data in persistent stores, with the exception of CreationDateContainer, and read in the new data. I think all of the code works as desired with one exception. I am unable to delete the data. I have written the function below that I thought would delete the associated persistent stores then the associated file but does not do so. The code compiles and executes the for loop.
func deleteManagedObjects() {
let arrayOfContainers: [String] = [
"VOOContainer",
"VFIAXContainer",
"PrincipalContainer"
]
for item in arrayOfContainers {
let container = NSPersistentContainer(name: item)
let storeCoordinator = container.persistentStoreCoordinator
for store in storeCoordinator.persistentStores {
try? storeCoordinator.destroyPersistentStore(
at: store.url!,
ofType: store.type,
options: nil)
try? FileManager.default.removeItem(at: store.url!)
}
_ = NSPersistentContainer(
name: item
)
}
}