SQLite and iOS: Advanced GRDB

In part 1: "Getting started with GRDB", we covered how to setup a local SQLite database for your iOS app, how to write migrations, how to adopt GRDB's protocols within a struct that can then be saved to the database, and lastly we went over some basic querying.

In part 2, we'll have a look at how to observe and react to changes in the database, how to define relationships between tables, and how to save a custom types.


Defining associations

To show you how associations work in GRDB, we'll create a tasks table and setup a "has many" relationship between the project and task tables. Tasks, in turn, will have a "belongs to" relationship with projects.

DatabaseManager.swift
1class DatabaseManager {
2
3 static var migrator: DatabaseMigrator {
4 var migrator = DatabaseMigrator()
5
6 migrator.eraseDatabaseOnSchemaChange = true
7
8 migrator.registerMigration("createProject") { db in
9 try db.create(table: "project") { t in
10 t.autoIncrementedPrimaryKey("id")
11 t.column("name", .text).notNull()
12 t.column("description", .text)
13 t.column("due", .date)
14 t.column("isDraft", .boolean).notNull().defaults(to: true)
15 }
16 }
17
18 migrator.registerMigration("createTask") { db in
19 try db.create(table: "task") { t in
20 t.autoIncrementedPrimaryKey("id")
21 t.column("projectId", .integer).notNull().indexed().references("project", onDelete: .cascade)
22 t.column("name", .text).notNull()
23 t.column("isDone", .boolean).notNull().defaults(to: false)
24 }
25 }
26
27 return migrator
28 }
29
30}

We went through what most of the above does in part 1; the interesting bit is line 21, where we create a column named projectId. The column is defined as an integer (because the primary key of the project table is an integer), then constrained to be notNull because we want SQLite to guarantee that all tasks have a project. indexed tells SQLite that we want the column to be indexed, which'll increase performance once we start querying for tasks that belong to a project (and thus matching against the value of this column). Lastly, references creates a foreign key constraint which tells SQLite that for each row in the tasks table, there exists a project it belongs to. onDelete: .cascade upholds that constraint by automatically deleting all tasks that belong a project when that project is deleted from the database.

Now that the required migrations have been done, create a Task struct for the newly created table (go back and reference part 1 if you're unsure how to do so). Once you've done that, we can move on to updating our Project struct.

Models/Project.swift
1extension Project: TableRecord, EncodableRecord {
2
3 static let tasks = hasMany(Task.self)
4
5 private enum Columns {
6 static let id = Column(CodingKeys.id)
7 static let name = Column(CodingKeys.name)
8 static let description = Column(CodingKeys.description)
9 static let due = Column(CodingKeys.due)
10 static let isDraft = Column(CodingKeys.isDraft)
11 }
12
13 var tasks: QueryInterfaceRequest<Task> {
14 return request(for: Project.tasks)
15 }
16
17 static func drafts() -> QueryInterfaceRequest<Project> {
18 return Project.filter(Columns.isDraft == true)
19 }
20
21}

In the above snippet, the extension that housed our queries in part 1 has been updated. First, on line 1, TableRecord and EncodableRecord have been adopted, which gives us access to GRDB's belongsTo function. On line 3, that function is then used to tell GRDB of the foreign key.

Lines 13 through 15 provide us with a nice way to query all the tasks that belong to a project simply by doing:

1let tasks = try project.tasks.fetchAll(db)

Similarly, we can update the Task struct to provide us with a way to fetch the project it belongs to:

Models/Project.swift
1extension Task: TableRecord, EncodableRecord {
2
3 static let project = belongsTo(Project.self)
4
5 var project: QueryInterfaceRequest<Project> {
6 return request(for: Task.project)
7 }
8
9}

To then fetch the parent project, all that's needed is:

1let project = try task.project.fetchOne(db)

Note that filters can still be applied when querying a hasMany relationship.The following, for example, is valid:

1let doneTasks = try project
2 .tasks
3 .filter(Column("isDone") == true)
4 .fetchAll(db)

Observing and reacting to changes

GRDB leverages SQLite's data change notifications to provide us with an efficient ValueObservation tool that will, in turn, call either the onChange or onError callback it is passed.

For this example, we'll implement an observer into a simple TaskListViewController whose view we want to update as and when the tasks that belong to a certain project update.

To start, import GRDB and define an optional TransactionObserver property in your controller; this is where our observer will be kept in memory. TransactionObserver is the type returned when start is called on a ValueObservation.

TaskListViewController.swift
1class TaskListViewController: UIViewController {
2
3 private var tasksObserver: TransactionObserver?
4
5}

tasksObserver now needs a value. We'll break its configuration out into its own function; remember to call configureTasksObserver in viewDidLoad, or wherever you're handling view setup.

TaskListViewController.swift
1class TaskListViewController: UIViewController {
2
3 private func configureTasksObserver() {
4 let project: Project = ...
5
6 let observation = ValueObservation.tracking { db in
7 try project.tasks.fetchAll(db)
8 }
9
10 tasksObserver = observation.start(
11 in: dbQueue,
12 onError: onTasksObserverError(_:),
13 onChange: onTasksObserverChange(_:)
14 )
15 }
16
17}

When start is first called on a ValueObservation type, the query in its definition will run once before any changes are made. This is nice because it means we don't have to duplicate the query elsewhere in our setup code to fetch the data required for the first initialization of the controller.

You can see that 2 callbacks are passed into start on lines 12 and 13. We still need to define those:

TaskListViewController.swift
1class TaskListViewController: UIViewController {
2
3 private func onTasksObserverChange(_ tasks: [Task]) {
4 // Update your UI, etc...
5 }
6
7 private func onTasksObserverError(_ error: Error) {
8 // Update your UI, etc...
9 }
10
11}

The only thing left to do is clean-up when the controller closes:

TaskListViewController.swift
1class TaskListViewController: UIViewController {
2
3 override func viewWillDisappear(_ animated: Bool) {
4 super.viewWillDisappear(animated)
5 tasksObserver = nil
6 }
7
8}

That's all there is to it; GRDB does a great job of making observation straightforward.

Tip: A frequest use case for database observation is keeping the data in a UICollectionView up to date. The DeepDiff package makes a great accompaniment to what we've just covered if that's what you're implementing.


Reading and writing custom data types

GRDB supports strings, integers, dates, booleans, and enums (that adopt DatabaseValueConvertible) out of the box. For this example, we'll contrive a requirement that projects can have a user-defined accent colour and banner image that must be modelled into a ProjectBrand struct.

DatabaseManager.swift
1class DatabaseManager {
2
3 static var migrator: DatabaseMigrator {
4 var migrator = DatabaseMigrator()
5
6 migrator.eraseDatabaseOnSchemaChange = true
7
8 migrator.registerMigration("createProject") { db in
9 try db.create(table: "project") { t in
10 t.autoIncrementedPrimaryKey("id")
11 t.column("name", .text).notNull()
12 t.column("description", .text)
13 t.column("due", .date)
14 t.column("isDraft", .boolean).notNull().defaults(to: true)
15 t.column("accentColor", .text)
16 t.column("bannerImage", .text)
17 }
18 }
19
20 return migrator
21 }
22
23}

You can see on lines 15 and 16 that the two new fields still have to adhere to the basic types when they're in the database, and that they are stored in their own columns.

GRDB will come into play post-read, transforming the two fields into one ProjectBrand, and pre-write, splitting the ProjectBrand back down to two fields in preparation for SQLite. Let's implement that.

Models/Project.swift
1struct Project {
2 var id: Int64?
3 var name: String
4 var description: String?
5 var due: Date?
6 var isDraft: Bool
7 var brand: ProjectBrand
8}
9
10struct ProjectBrand {
11 var accentColor: String?
12 var bannerImage: String?
13}
14
15extension Project: TableRecord, FetchableRecord, MutablePersistableRecord {
16
17 enum Columns: String, ColumnExpression {
18 case id, name, description, due, isDraft, accentColor, bannerImage
19 }
20
21 init(row: Row) {
22 id = row[Columns.id]
23 name = row[Columns.name]
24 description = row[Columns.description]
25 due = row[Columns.due]
26 isDraft = row[Columns.isDraft]
27 brand = ProjectBrand(
28 accentColor: row[Columns.accentColor],
29 bannerImage: row[Columns.bannerImage]
30 )
31
32 super.init(row: row)
33 }
34
35 func encode(to container: inout PersistenceContainer) {
36 container[Columns.id] = id
37 container[Columns.name] = name
38 container[Columns.description] = description
39 container[Columns.due] = due
40 container[Columns.isDraft] = isDraft
41 container[Columns.accentColor] = brand.accentColor
42 container[Columns.bannerImage] = brand.bannerImage
43 }
44
45 mutating func didInsert(with rowID: Int64, for column: String?) {
46 id = rowID
47 }
48
49}

You'll see on line 7 that instead of accentColor and bannerImage keys in the Project struct, we have a brand key of type ProjectBrand. Your struct should represent your data as you want to use it in your app, now how it is structured in the database.

The two main differences when compared to the Project struct we wrote in part 1 are the init and encode functions. The former is responsible for modelling a database row into a Project when the database is read; you can see that lines 27 through 30 transform the accentColor and bannerImage rows into a ProjectBrand. The latter is responsible for splitting the Project back down into database rows — lines 41 and 42 break down the ProjectBrand.

With all that in place, here's an example of what a Project could look like in your app:

1try dbQueue.write { db in
2 var projectBrand = ProjectBrand(
3 accentColor: "#005eff",
4 bannerImage: nil
5 )
6
7 var project = Project(
8 name: "Advanced GRDB",
9 description: "A blog post",
10 due: Date().addingTimeInterval(24 * 60 * 60),
11 isDraft: true,
12 brand: projectBrand
13 )
14
15 try! project.insert(db)
16}

You can see that we can now call insert directly on a Project type even though it has a key that uses a custom data type. As we're writing to the database in this case, the afore-defined encode function will run as part of the write process and breakdown the ProjectBrand. Similarly, we could read a Project and the ProjectBrand would be pre-assembled for us by the init function.

1try dbQueue.read { db in
2 let project = try Project.fetchOne(db, key: 1)
3 print(project)
4
5 // => Project(id: nil, name: "Advanced GRDB", description: Optional("A blog post"), due: Optional(2019-12-11 14:16:15 +0000), isDraft: true, brand: ProjectBrand(accentColor: "#005eff", bannerImage: nil))
6}

Wrapping up

I hope this 2 part series — the first series I've ever published on this site — has helped you get to grips with SQLite on iOS, and shown you its power and ease of use when paired with GRDB.

There's no comment section on here, but I cross-post to dev.to which does have one. If you have any questions, feel free to ask there.

Thanks for reading.