I'm reading Delete/Reset all entries in Core Data?.
If I follow the steps below, I get an unexpected result:
- Call the code below
- Then query an entity in the simulator, I will get an entity back!!!
- If I call
clearCoreDataStore
again (or do a restart), only then the value won't be retrieved from core-data
What am I missing?
func clearCoreDataStore() {
let entities = dataManager.persistentContainer.managedObjectModel.entities
for entity in entities {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entity.name!)
let deleteReqest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do {
try context.execute(deleteReqest)
} catch {
print(error)
}
}
dataManager.saveContext()
}
persistentContainer
property. All operations are using the context of thepersistentContainer
property... – SwitchboardsaveContext
. Yet if I call myclearCoreDataStore
and query ... I will get a result back. But if I call again, then nothing would be returned. Not sure why I need to do it twice... – Switchboard