Is there a safe way to remove persistent storage (and create a new one) in an application where other threads use the NSManagedObjectContext associated with the deleted storage?
This should be safe if you can ensure that no live managed objects try to access persistent storage. This part is crucial: you have to make sure that there are no live objects of any type that have been downloaded or linked to the old persistent storage or otherwise connected to it.
You can do this by dropping each context of the managed entity:
[managedObjectContext performBlockAndWait:^{ [managedObjectContext reset]
Once you have done this for each context of the managed entity, you can delete the persistent storage.
Note that if you have any managed objects that have been extracted from these contexts, you must destroy them immediately without reading or writing the attribute values ββor using them in any way. These objects may need to use context for various reasons, but after calling reset context knows nothing more about them. Get rid of them right away (ideally even before calling reset), because they are time bombs waiting to explode your application the moment you touch them.
source share