sqlite database disk image malformed on iPhone SDK

前端 未结 3 1778
無奈伤痛
無奈伤痛 2021-01-12 07:58

I\'m having an issue with a new application on the iPhone SDK using SQLite as the DB backend.

Occasionally, my app will stop loading data to my UITableViews and afte

相关标签:
3条回答
  • 2021-01-12 08:41

    You have to be very careful about background threads accessing the database while debugging! This is because when the debugger halts processing (such as at a breakpoint) all threads are paused, including threads that may be in the middle of a database call, somewhere in between a database "open" and a database "close" call.

    If you are halted at a breakpoint, and click the stop sign in Xcode, your app exits immediately. This very often causes errors such as the one you saw, or the "corrupted database" error.

    There really isn't any solution (because there is no way to modify the behavior of "stop tasks", but I have evolved some techniques to mitigate it: 1. Add code to detect the app entering the background and have your db operations gracefully stop. 2. Never use the stop sign to halt processing while debugging. Instead, when done with a breakpoint then "continue", hit the home button on the simulator or device (which should trigger the code you added in step 1), wait for the app to background, THEN you can stop the run.

    0 讨论(0)
  • 2021-01-12 08:53

    In my case this had to do with iOS 7: On iOS 7 Core Data now uses the SQLite WAL journaling mode which writes data to a .db-wal file instead of directly to the .db file. In my app, I would copy a prepared .db file into Library/Application Support during an app update. The problem was that an old .db-wal file was still in that directory and I only replaced the .db file. That way, I ended up with a .db file that was out of sync with the old .db-wal file.

    There are two solutions to this problem:

    1. Make sure the .db, .db-wal and .db-shm files are deleted before you copy your new .db file into place.
    2. Go back to the old pre-iOS 7 behavior like so: https://stackoverflow.com/a/18870738/171933
    0 讨论(0)
  • 2021-01-12 08:55

    Depends on how SQLite is compiled, it may or may not be thread-safe. If you're using the built-in one, it may not have the compile-time options you're looking for.

    For our app, we had to roll our own SQLite to add full text search. Take a look at this page.

    0 讨论(0)
提交回复
热议问题