Single Model, Multiple Persistent Stores

I have an application that is project based. Each project will have its own persistent store, but there is only one model that is used for all the projects. Only one project will be in use at any given time. I know I can set the persistentStoreDescriptions URL to store location prior to the store being loaded by loadPersistentStores. However, it is not clear to me how one can change the persistent store after it has been loaded. Case in point, PROJECT1 is loaded and some work is done then you want to load PROJECT2 and work on it. Both the managedObjectContext and the store need to be replaced with new ones. How do you do that within the persistentcontainer framework


Thanks,


Jim

Replies

Doesn't it still work to access the persistent store coordinator and use the 'configuring persistent stores' methods to add and remove persistent stores?


As far as creating a new managed object context, you shouldn't really need to create a new one. You should be able to just reset it after changing the persistent stores.


And, to be honest, before NSPersistentContainer, you would have just thrown away your managed object context and maybe your entire core data stack, depending on how you arranged your code, when switching peristent stores. So the most extreme scenario, throwing away your NSPersistentCoordinator and initializing a new one passing the new managed object context around isn't terribly extreme.