Correct way to refresh core data stack

I have a Core Data macOS app with a facility to download a saved store (sqlite files etc.) from dropbox. I am able to download the myProject.sqlite, myProject.sqlite-wal and myProject.sqlite-shm files to the correct folder. Restarting the app shows the correct data. However, I want the new data to appear immediately.


What is the best way to reload the data without having to restart the app?

Replies

First, go out on the Internet and find the answer to the impossible question "What's the best way to organize your Core Data stack?"

Then, you can ask "What's the best way to reload that Core Data stack?"


But the most practical answer: Use global notifications. Issue a global notification when you invalidate and rebuild your stack. And make each of your view controllers register for the global notification so they know to either refresh or dismiss if they are now displaying invalid data.

Because the "best answer" has to deal with issues like "I was displaying entity X, how do I find X in my new data?"

Hello nigelfromtewkesbury,

Core Data is a closed, black-box system. While it is usually implemented with SQLite, it doesn't have to be. Plus, Apple can change that implementation or the SQLite schema at any time. The best idea is to avoid anything like what you describe. If you still want to support this kind of reloading via dropbox, you should definine your own persistence schema using XML or JSON and use that for saving and restoring via dropbox. At that point, reloading the data in the app is trivial.