I'm storing some sensitive information like passwords etc. in Core Data. I want that my app encrypts the entire SQLite database (it's not very big, < 1 MB) whenever the apps goes into the background or terminates. I figured out the encryption thing itself, but I'm having problems to correctly 'close' and re-open the Core Data stack with the store.
When my apps terminates/goes into the background I do this now:
- Save the context
- Remove the store from the coordinator
- Encrypt the store and save it
- Delete the store
When my app comes back, I do the following:
- Decrypt and save the store file
- Add the store back to the coordinator
- Reset the context
From what I understood from the docs this should be sufficient, but it doesn't work, as soon as the main view controller tries to do a fetch on the context again my app crashes.
Does anybody know of what is the best way to temporarily remove a store from core data and then add it again?