🎉 End-of-year Sale! Save 25% when you subscribe today.

SQLite: Observation

Episode #304 • Nov 25, 2024 • Subscriber-Only

We conclude our introductory series on SQLite by showing how to live update SwiftUI views powered by database using GRDB’s “value observation.” Along the way we will tackle quite a few Swift 6 concurrency issues, and we will tie things in a bow by showing how the SwiftUI environment can vastly simplify how our application is powered by SQLite.

Previous episode
SQLite: Observation
Next episode
Locked

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Sign in with GitHub

Introduction

Brandon

With just a little bit of work we have modeled a new domain type for our application, created a relationship between domain types, and created a new view to display the data for this new type, as well as navigation to the view. And this sets the stage for how one can build a simple SQLite-based application. In the onAppear of a feature’s view you can execute some queries to populate the data in the view, and then SwiftUI takes care of the rest.

Stephen

But also most apps cannot be so simple. Typically you will have lots of features accessing the database, and then also lots of features writing to the database. And when one features makes a write you will want to make sure that all other views update in order to show the freshest data.

This brings us to a powerful feature that SQLite has called “change notifications”. We didn’t explore this when dealing with the C library directly because it can be quite cumbersome due to how one handles callback functions in C. But luckily GRDB has a nice interface to this functionality, so let’s take a look.

Database observation


References

Downloads

Sample code

Get started with our free plan

Our free plan includes 1 subscriber-only episode of your choice, access to 64 free episodes with transcripts and code samples, and weekly updates from our newsletter.

View plans and pricing