Search code examples
iossqliteios5ios-simulatornsfilemanager

Sqlite file deletes on reset of simulator


I am using sqlite in my IOS project. I am copying the sqlite file from my main bundle to documents directory on launch of the application. It does copy, but suppose if I reset the simulator, the sqlite file from documents directory is also getting deleted, thus all the changes made goes away with it.

Any help would be appreciated.


Solution

  • First, you can readup here on where to save persistent data, and how to prevent iCloud backups. In your case of the simulator, you need to add code to your program wrapped in an 'ifdef SIMULATOR' (I don't have a reference handy on what define to test against, but I know there is one), and save a copy of your file to a fully qualified path that leads to your user account (ie /Users/xyz/iOS/xzy.data). When your app starts up, you first look for the file in the appropriate place as you would on the device. Then, if the file is not found, for the Simulator only, you see if there is a file in that hard coded path, and if so copy it back to the Simulator.