M-Swift All articles
Swift Architecture

Picking a Local Store You Won't Regret: SwiftData and GRDB for Offline-First Apps

M-Swift
Picking a Local Store You Won't Regret: SwiftData and GRDB for Offline-First Apps

Photo: developer comparing database schema diagrams on laptop in modern office, via i.etsystatic.com

Offline-first sounds simple until the first sync conflict hits a device in a warehouse with one bar of signal. Then the database choice stops being a matter of taste and becomes an architectural liability. SwiftData and GRDB can both get the job done in 2026, but they ask different things of your team. One wants you to stay inside Apple's model layer. The other wants you to think in SQL and transactions. Neither is free.

At M-Swift, we are less interested in a drag race between two persistence layers than in the question that actually matters: which one will your team debug calmly at 11 p.m. when the local store and the server disagree? So skip the synthetic benchmarks. Start with constraints.

Start With the Questions You Can't Avoid

Before you compare APIs, write down the awkward parts of your app. How many entities have real relationships? Do you need full-text search, aggregate reports, or bulk upserts? How often does the schema change? Are background imports normal or exceptional? Can a new engineer write a reliable test against the store without reading three wiki pages?

If the answers are mostly simple CRUD with SwiftUI views, SwiftData has a short runway. If the answers include complex queries, precise migrations, and sync diagnostics, GRDB starts looking less like extra work and more like insurance.

The Decision Matrix

Constraint SwiftData edge GRDB edge Practical call
Simple CRUD with SwiftUI Very little boilerplate More setup SwiftData
Complex reporting queries Limited SQL control Full SQL, CTEs, indexes GRDB
Schema churn Lightweight migrations are easy Explicit migrator, testable steps GRDB for messy history
Background writes Model actors help, but context rules need discipline Database queues and transactions are explicit GRDB
Test fixtures In-memory containers are convenient In-memory queues are deterministic Tie, lean GRDB
Debugging bad sync Inspect models, but less SQL visibility Query the store directly GRDB
Small team, Apple-only stack Lower cognitive load SQL knowledge required SwiftData
Long-lived app with auditors Less control over exact schema Migrations become documentation GRDB

That table is not a scoreboard. It is a warning system. If three or more rows point to GRDB, do not pick SwiftData just because the first prototype felt fast.

The Same Schema, Two Personalities

Imagine a field-service app with job sites and work orders. In SwiftData, the model layer is the schema:

@Model
final class JobSite {
    @Attribute(.unique) var id: UUID
    var name: String
    var updatedAt: Date
    @Relationship(deleteRule: .cascade) var workOrders: [WorkOrder] = []
}

@Model
final class WorkOrder {
    @Attribute(.unique) var id: UUID
    var title: String
    var status: String
    var jobSite: JobSite?
}

That is pleasant. The relationship is visible, the model works with SwiftUI, and you can fetch with a predicate without writing SQL. The tradeoff shows up when someone asks for a report of open work orders grouped by job site and sorted by the oldest update. You can compose it, but you are composing through SwiftData's query layer, not writing the exact SQL you might want.

GRDB makes the schema and the mapping explicit:

struct JobSite: Codable, FetchableRecord, PersistableRecord {
    static let databaseTableName = "jobSite"
    var id: UUID
    var name: String
    var updatedAt: Date
    static let workOrders = hasMany(WorkOrder.self)
}

struct WorkOrder: Codable, FetchableRecord, PersistableRecord {
    static let databaseTableName = "workOrder"
    var id: UUID
    var jobSiteId: UUID
    var title: String
    var status: String
    static let jobSite = belongsTo(JobSite.self)
}

More lines, yes. But every line maps to a table, a column, or a relationship you can inspect with a SQL browser. When sync goes sideways, that visibility is not academic.

Where SwiftData Feels Great

SwiftData shines in apps where the model graph is the product and the queries stay close to it. A small team building a SwiftUI-first app can move quickly, use previews, and avoid maintaining a separate persistence layer. For straightforward offline caches, drafts, or personal data, the convenience is real.

The red flags are equally real. If you need bulk upserts that touch thousands of rows, custom indexes, full-text search, or reports that read like SQL homework, SwiftData starts fighting you. Background imports need careful context isolation, and duplicate writes can be harder to reason about than a single transaction. Test isolation also deserves scrutiny. An in-memory container is convenient, but shared state between tests can hide ordering bugs.

Where GRDB Earns Its Keep

GRDB is a better fit when the local store is a serious part of the product. You get explicit migrations, transactions, custom SQL, and observation that does not pretend the database is invisible. Complex queries are not a workaround. They are the normal path. When sync breaks, you can open the SQLite file, run the same query the app runs, and compare the results without guessing what the framework did underneath.

The cost is discipline. Your team needs to be comfortable with SQL, migrations, and mapping rows to types. Relationships are manual. If nobody owns the schema, GRDB will happily let you build a mess with excellent query performance.

Background Writes and Sync Failures

Both tools can handle background work. The question is whether the failure mode is obvious. With SwiftData, model actors and separate contexts help, but you need a clear rule about which context owns which write. With GRDB, database queues and transactions make the boundary explicit. Neither approach removes sync complexity, but GRDB makes it easier to prove what happened after the fact.

Testability Is a Design Smell

Write one test that seeds a store, performs a sync merge, and asserts the final state. If the test requires a lot of ceremony just to avoid leaking state, that is a signal. SwiftData can be tested cleanly, but the setup needs intention. GRDB's in-memory database and migration tests tend to be more deterministic, especially when you need to prove that an old store upgrades correctly.

Migration Posture Wins Arguments

SwiftData's lightweight migrations are great until they are not. Complex changes need versioned schemas and custom migration logic, and that logic deserves tests. GRDB's migrator forces you to name each step. That feels bureaucratic early on and invaluable two years later when a support engineer asks why a customer's store has a column nobody remembers adding.

The Call

There is no universal winner. Pick SwiftData when the app is SwiftUI-heavy, the model graph is modest, and the team values lower ceremony. Pick GRDB when queries are part of the product, migrations are frequent, sync debugging matters, and the team is willing to own SQL. The worst outcome is picking by default and discovering the mismatch during a production incident. Choose deliberately, write down why, and revisit the decision when the schema stops being simple.

All Articles

Related Articles

The Red Dot Doesn't Lie — Except When It Does: Reconciling Notification Badges in Swift

The Red Dot Doesn't Lie — Except When It Does: Reconciling Notification Badges in Swift

Hard-Coded Hex and Rogue Padding: Auditing Your SwiftUI Design System Before the Redesign Lands

Hard-Coded Hex and Rogue Padding: Auditing Your SwiftUI Design System Before the Redesign Lands

Feature Flags Without the Baggage: A Swift-Native Rollout Layer

Feature Flags Without the Baggage: A Swift-Native Rollout Layer