Make SQLite Writes Atomic Across An App And Its Widget
Your app marks a task complete and records a change for its widget. Those are two SQL statements, but they represent one user action. If the second statement fails, a completed task without the matching change record is an application bug.
Putting both calls inside a Swift actor does not solve that problem. An actor serializes access to its own state within a process. The widget has another process, another connection, and its own lifetime.
Define the invariant before choosing a lock
For this example, completing a task must update tasks and insert a row into changes together. The schema is deliberately small:
CREATE TABLE tasks (
id INTEGER PRIMARY KEY,
completed INTEGER NOT NULL
);
CREATE TABLE changes (
operation_id TEXT PRIMARY KEY,
task_id INTEGER NOT NULL
);
A unique operation ID gives the example a convenient failure case: try to record the same operation twice. This is not a complete sync protocol. Its purpose is to make a partial write easy to detect.
SQLite’s transaction documentation describes the boundary we need: commit related statements as a unit, or roll them back.
BEGIN IMMEDIATE;
UPDATE tasks SET completed = 1 WHERE id = ?;
INSERT INTO changes(operation_id, task_id) VALUES (?, ?);
COMMIT;
Bind all three values through your SQLite wrapper. Check that the UPDATE affected the expected task before inserting its change record. If any step fails, the wrapper must end the transaction according to its documented rollback policy; do not leave the connection holding a transaction while showing an error alert.
Keep the transaction short
BEGIN IMMEDIATE attempts to acquire the write transaction at the start. It can report SQLITE_BUSY when another connection already has a writer. That moves contention to a deliberate boundary instead of discovering it halfway through application work.
Resolve network requests, file decoding, and user confirmation before entering this boundary. Do not suspend for an unrelated await while holding the transaction. A busy timeout is a bounded waiting policy, not a reason to block the main thread indefinitely.
Handle commit errors too. Depending on the error, a transaction may remain active. A database library’s transaction closure is useful precisely because it centralizes those ownership and cleanup rules. Inspect its behavior instead of assuming every thrown error automatically rolls back SQLite.
Give the widget a consistent read
Enable the same App Group for the relevant targets and obtain the container URL through FileManager. Do not hardcode a simulator container path. App Group membership provides access; it does not make a pair of SQL statements atomic.
With write-ahead logging, a reader can continue seeing a committed snapshot while a writer works. WAL still permits only one writer at a time.
If the widget needs several related values, read them in one SELECT or an explicit read transaction. Two independent reads can straddle a commit and observe different snapshots even when every write transaction is correct. Finish read transactions promptly so a long-lived reader does not unnecessarily hold an old snapshot.
Reproduce the failure with two connections
The companion fixture uses a temporary file and separate writer and reader connections. It verifies four observable outcomes:
- After the writer updates the task but before commit, the reader still sees the old task and zero change records.
- A second
BEGIN IMMEDIATEreports writer contention. - After commit, the reader sees the completed task and its change record together.
- A duplicate operation ID fails, rollback runs, and neither half of the attempted edit survives.
The reader checks both values with one SELECT. That detail makes the assertion about a single snapshot meaningful.
Keep the database’s WAL and shared-memory sidecars under SQLite’s control. Copying only the main database file while it is open is not a backup strategy; use your library’s supported backup mechanism. If Core Data or SwiftData owns the schema, use that framework’s transaction and sharing facilities rather than applying this SQL behind its back.
September 2026 review: the two-connection fixture passed on the host SQLite library. It tests transaction visibility and failure handling, not widget entitlement setup, process termination, or device filesystem protection. Those remain integration checks for the actual app.