First of all, I'm not using Core Data, I'm using SQLite only.

I save the data in the table and then query them by opening the app and looking at the data using SQLiteStudio. I even create a LOG to check that the data is saved, and yes, it is saved. But when I STOP and run the simulator again to consult, there is no more data!

Is Xcode overwriting the database every time? Or is something else happening that is outside my limited knowledge?

有帮助吗?

解决方案

Without knowing more about how your application is setup, how you've got your app configured to build, or how it operates on the SQLite DB file in question I'll only be able to offer some pointers in things you can go check out about your code and build configuration that may be the source of your phantom deletions. Of course, if you have other info to provide, I'd be happy to edit my answer!

Case 1: 'Create DB' always running?

One thing that may be tripping your app up is what happens leading up to the decision to create a new SQLite DB file or look/open an existing file. If the code creating an empty DB is always running, then each time your app starts, your old DB file is getting overwritten with a blank DB.

Case 2: Using a 'starter' or 'template' empty database?

Sometimes developers may provide a blank database that contains the initial database schema (the general tables and structure) as well as some default or sample data. If your app does this, perhaps the logic leading up to the decision to apply that default database is accidentally always being triggered? If so, use of NSUserDefaults to record a boolean indicating the DB was successfully created may be an avenue to use to skip past the 'Load my starter DB' code. Alternatively, you could check for the existence of your DB file, or see if the contents of a specific table are different from the template data, etc.

Case 3: Different Behavior between 'Build & Run' vs. 'Run Without Building'

There's a not-so-well-known option in the Product > Perform Action menu labelled 'Run without Building' that will essentially kickoff another Debug session using the version of the application you just finished running in the Simulator or on Device. When you use this option do you see any different behavior with your database or is it still blank?

Case 4: Different Behavior when run directly in Simulator outside of an Xcode debugging session?

Part of the 'Run' operation is a build phase which may trigger the 'Copy Resources' phase even if your app hasn't changed since the last execution (as you suggest is the case in your question). If you are providing a stock 'default' or 'template' DB file and your app is simply opening and editing that 'template' during the first execution of your app, then Xcode may be replacing it with a clean copy on the subsequent 'Run' operations where 'Copy Resources' is happening. A way to test this avenue:

  1. Build and Run your app to the simulator using Xcode like normal.
  2. Perform some operations that would result in the creation or editing of data in your app's database.
  3. Click the stop button in Xcode to return to the Simulator home screen.
  4. Double-click the home button on the simulator (or if there is no home button, press CMD+SHIFT+H twice to bring up the multitasking bar and force-quit out of your application.
  5. Check and see if your DB file has data in it.
  6. If no data, then there is an issue persisting your changes into the database and we need to get that problem solved first. Otherwise:
  7. Relaunch your app directly from iOS Simulator and perform different operations that would result in more or different changes to the database.
  8. Click on the home button to return to the iOS Home Screen.
  9. Force-quit your appellation as was done in Step 4.
  10. Check and see if your DB file data has changed (but still has data) or has blanked out.

Finally, make sure you are following Apple's guidance about where to store user-data, if you are inadvertently storing something in an incorrect file path doesn't typically result in blanking of data, it may be prohibiting writing of data which could be interpreted as your data getting overwritten especially if you are interrogating it while it still is residing in an in-memory process. There's some really useful guidance about file paths in the Table 1-1: (http://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html)

Locating your Simulator App on your Mac's Hard Drive

To be thorough (and you may already know about this!), iOS Simulator applications are stored on your Mac's hard drive just like other files on your machine. Their organization mimics that of a physical iOS device. To get to your App and its data:

  1. Open a new Finder window.
  2. Press CMD+SHIFT+G or choose 'Go to Folder' from the 'Go' menu.
  3. Paste the following into the 'Go to Folder' box then click 'Go': ~/Library/Application Support/iPhone Simulator/
  4. Select the folder that matches the iOS version of the simulator you built to.
  5. Click on Appications.
  6. You'll then be presented with zero or more folders, each folder that appears will have a string of digits separated by hyphens. Navigate through this list until you find the one containing your app. You can then browse, and copy data out of this folder to somewhere to be examined by other tools on your Mac.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top