Crashes when trying to destroy persistent store

I am running into some issues when trying to destroy CoreData persistentStores. When a user logs out of my app, I want to completely reset CoreData and delete any existing data. My code to reset CoreData looks like this:

let coordinator = self.persistentContainer.persistentStoreCoordinator
self.persistentContainer.viewContext.reset()

coordinator.persistentStores.forEach { store in
     guard let url = store.url else { return }

     do {
         try coordinator.destroyPersistentStore(at: url, type: .sqlite)
         _ = try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url)
     } catch {
         print(error)
     }
}

However, my app is crashing with

Object 0xb2b5cc80445813de <x-coredata://BDB999D4-49A4-4CB3-AC3A-666AD60BEFC6/AccountEntity/p5> persistent store is not reachable from this NSManagedObjectContext's coordinator

It seems this is related to the SwiftUI @FetchRequest wrappers. If I do not open the views where I am using @FetchRequest, the logout goes smoothly. Otherwise, I get the crash above.

Has anyone run into anything similar? Is there something else I need to do to get the underlying FRC to release its references to those entities? I was under the impression that calling reset() on the managed object context would be enough to remove those items from memory and get the destroying of the persistent store to go smoothly.

Alternately, is there another/better way I should be destroying the DB?

Any advice or related observations would be greatly appreciated. Thank you!

Answered by DTS Engineer in 826452022

The error message indicates that your code was accessing the persistent store when the store was not reachable, which could be the case if your SwiftUI + @FetchRequest fetched data from the store after destroyPersistentStore(at:type:options:) had removed the store from the Core Data stack.

FetchRequest is highly encapsulated. There is no way to have it not fetch when the store doesn't exist. You will need to use your own state to avoid accessing the store in that situation, which probably means that you remove the view hierarchy that can access the store before calling destroyPersistentStore(at:type:options:).

viewContext.reset() resets viewContext, which I don't think is relevant to the error.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

If anyone else runs into this, what I ended up doing was destroying the store on appearance of the "Login" page the user is taken to upon logging out. I didn't do anything special to make sure it only happens in that specific scenario since there is nothing I store in CoreData while the user is logged out, so it is always safe to call that if the user is logged out.

The error message indicates that your code was accessing the persistent store when the store was not reachable, which could be the case if your SwiftUI + @FetchRequest fetched data from the store after destroyPersistentStore(at:type:options:) had removed the store from the Core Data stack.

FetchRequest is highly encapsulated. There is no way to have it not fetch when the store doesn't exist. You will need to use your own state to avoid accessing the store in that situation, which probably means that you remove the view hierarchy that can access the store before calling destroyPersistentStore(at:type:options:).

viewContext.reset() resets viewContext, which I don't think is relevant to the error.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Crashes when trying to destroy persistent store
 
 
Q