Main Thread Model, Serial Queue Database
Is there a name for this particular pattern?
This is how I do things in NetNewsWire — it’s a pattern I’ve used in previous apps, too. (Vesper and Glassboard, for instance.)
The idea is that the model objects live on the main thread, and all database access happens on a serial queue. This way everything stays in sync, with the added benefit of being able to not block the main thread when doing database updates and queries.
There are some details worth knowing. The database queue makes model objects, then passes them back to the main thread. Most of the time those model objects are immutable structs, so memory sharing isn’t an issue.
But, when they’re not — when those objects are mutable — the main thread takes ownership of the model objects, once they’ve been created on the database queue and passed to the main thread. The database queue is not permitted to mutate those objects.
How it works in my app
In NetNewsWire, articles are stored in a SQLite database. (Feed data is stored in an OPML file, with metadata in a side-file plist.) An article is really an Article
with optional Author
and Attachment
objects — and those are all immutable structs, so they’re fine.
But an Article
also has a single required ArticleStatus
object — and that one is a mutable object, since read/unread/starred status is a thing that changes pretty often.
They’re all created on the database queue and then passed to the main thread, which then owns them.
So, again, my question
What’s the name for this particular pattern?
PS I made the diagram in OmniGraffle — I’ve been enjoying learning the app. It feels like a superpower to be able to show something that I would have been able to describe only in words.
PPS I don’t claim to have made beautiful color choices. :)
PPPS If the diagram looks a little blurry, it’s because it’s been scrunched-up for this web page. Sorry about that! I need to learn how to make that not happen.