Fetching Objects with FMDB and SQLite →
https://marco.org/2014/03/29/fetching-objects-with-fmdb-and-sqlite
To continue the ongoing blog conversations between Brent Simmons and me, I’d like to point out that the system he describes here is extremely similar to my open-source FCModel.
In response to some of the notes at the bottom:
- FCModel supports primitive (non-object) properties for exactly the reason Brent describes: internally, it’s using
setValue:forKey:
. - FCModel supports custom serialization of any property via
serializedDatabaseRepresentationOfValue:...
, as documented in the header. (Too much of the documentation is still just in the header.) By default, like in FMDB,NSDate
values are converted to/from Unix timestamps for storage. (I recognize that isn’t a perfect translation, but it’s a reasonable default.) - FCModel does use property introspection. See here: after opening the database and performing any needed schema migrations, FCModel reads all of the tables and object properties, does some basic checks, tracks things like default values and whether
NULL
is allowed, and warns if there’s anything weird in the schema.
If anyone’s curious about FCModel, I encourage you to read the brief README
, FCModel.h
, and maybe even FCModel.m
if you want to see the inner workings. I’ve intentionally kept FCModel small, as a design goal, for this purpose. Core Data offers much more functionality but much more complexity and opaqueness — FCModel is for people who want the opposite, and would rather just write SELECT
queries (and occasional bulk UPDATE
s) in compact, readable SQL.
As an aside, I’m immensely proud of FCModel so far. It’s my most useful open-source contribution to date by far (in my opinion), we’ve gotten many great contributions, and a handful of people are already using it to build and ship apps. (I’m building Overcast with it, of course.)