ccgus/fmdb — pull request #327 by blakewatters (2014-12-19 02:40:31+00:00)
Title: Single-writer/Multi-reader Classes: Object Transaction and Read Only Database Pool

Hey there -

I wanted to offer up some FMDB extension classes that my team has been relying on internally to develop LayerKit. Our framework has two main pieces: a sync engine that is running constantly and a public API that performs database queries and mutations to drive synchronization activities. Because of the amount of concurrency on-going at any given time, we have opted to implement single writer / multi-reader on top of FMDB. These classes provide a few things:

1. The `FMDatabaseTransaction` class provides an object interface for mediating concurrent access to a single writable connection. It uses a semaphore to ensure that only one object can acquire write access to the database at any time. This is a more convenient interface for us than blocks in some cases because of how write access flows through the sync system.
2. The `FMReadOnlyDatabasePool` class provides an interface for mediating access to a finite pool of readers. Under the hood a hash map table of proxy objects guarantees that connections return to the pool as objects are reaped by ARC.

There’s some obvious overlap with the `FMDatabaseQueue` implementation, but this API is tailored to the reader use-case and the automatic management of connections returning to the pool is very convenient in practice. Anyway, we thought we’d share and see if there’s interest. Happy to make any adjustments necessary. I also have some test cases for the classes, but they are dependent on Expecta so I didn’t pull them in yet.