Question

I have a Data Access Object (DAO) on top of an sqlite database. This DAO has:

  • Read methods - to list or retrieve items according to some criteria.
  • Save methods.

And it happens that there's some contention:

  • There's a background indexing service that writes to the database on a background queue.
  • There's a background service that loads the thumbnail for an image record, on a background queue.

Now, we're noticing that if the indexing service is running then the reading of image thumbnails is noticibly slower.

The reason for this is that we're using the FMDB objective-C wrapper to sqlite. And it has a synch queue to the database, to ensure thread-safety.

Is there a common approach to prioritize sqlite reads over writes, especially with FMDB?

Was it helpful?

Solution

Depending on how you open your database (i.e.: with .shm and .wal concurrency) this might be less of a problem now? Otherwise, an option might be to add your own abstraction (i.e: JBReadWriteDatabaseQueue) which internally has both a read and a write arrays of blocks (or their own queues). It could then expose methods like readInDatabase: and writeInDatabase which might pass on the block directly to the fmdb queue if nothing is running, or store them if it is running. On completion of the current fmdb block, this class could preferentially pick from the read block arrays if there were entries there.

This does then get further into fun cases like write starvation if the read array never clears, but hey, such is life in concurrency eh?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top