Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interprocess SQLite Thread Safety (on iOS)

I'm trying to determine if my sqlite access to a database is thread-safe on iOS. I'm writing a non App Store app (or possibly a launch daemon), so Apple's approval isn't an issue. The database in question is the built-in sms.db, so for sure the OS is also accessing this database for reading and writing. I only want to be able to safely read it.

I've read this about reading from multiple processes with sqlite:

Multiple processes can have the same database open at the same time. Multiple processes can be doing a SELECT at the same time. But only one process can be making changes to the database at any moment in time, however.

I understand that thread-safety can be compiled out of sqlite, and that sqlite3_threadsafe() can be used to test for this. Running this on iOS 5.0.1

int safe = sqlite3_threadsafe();

yields a result of 2. According to this, that means mutex locking is available. But, that doesn't necessarily mean it's in use.

I'm not entirely clear on whether thread-safety is dynamically enabled on a per connection, per database, or global basis.

I have also read this. It looks like sqlite3_config() can be used to enable safe multi-threading, but of course, I have no control, or visibility into how the OS itself may have used this call (do I?). If I were to make that call again in my app, would it make it safe to read the database, or would it only deconflict concurrent access for multiple threads in my app that used the same sqlite3 database handle?

Anyway, my question is ...

can I safely read this database that's also accessed by iOS, and if so, how?

like image 240
Nate Avatar asked Jun 15 '12 20:06

Nate


People also ask

Is SQLite connection thread-safe?

In serialized mode, SQLite can be safely used by multiple threads with no restriction.

What is thread-safe in iOS?

Thread Safety APIs in iOSBy creating a queue of tasks where only one task can be processed at any given time, you are indirectly introducing thread safety to the component that is using the queue.

Does SQLite work on iOS?

SQLite is available by default on iOS. In fact, if you've used Core Data before, you've already used SQLite.


1 Answers

I've never used SQLite, but I've spent a decent amount of time reading its docs because I plan on using it in the future (and the docs are interesting). I'd say that thread safety is independent of whether multiple processes can access the same database file at once. SQLite, regardless of what threading mode it is in, will lock the database file, so that multiple processes can read from the database at once but only one can write.

Thread safety only affects how your process can use SQLite. Without any thread safety, you can only call SQLite functions from one thread. But it should still, say, take an EXCLUSIVE lock before writing, so that other processes can't corrupt the database file. Thread safety just protects data in your process's memory from getting corrupted if you use multiple threads. So I don't think you ever need to worry about what another process (in this case iOS) is doing with an SQLite database.

Edit: To clarify, any time you write to the database, including a plain INSERT/UPDATE/DELETE, it will automatically take an EXCLUSIVE lock, write to the database, then release the lock. (And it actually takes a SHARED lock, then a RESERVED lock, then a PENDING lock, then an EXCLUSIVE lock before writing.) By default, if the database is already locked (say from another process), then SQLite will return SQLITE_BUSY without waiting. You can call sqlite3_busy_timeout() to tell it to wait longer.

like image 81
Jordan Miner Avatar answered Oct 23 '22 19:10

Jordan Miner