From f80f59cc382eb46d00fc6a828b55f16819ee1538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 17 Nov 2018 18:41:46 +0100 Subject: [PATCH] Tweaks to demo app --- .../GRDBDemoiOS/GRDBDemoiOS/AppDatabase.swift | 15 ++++++--------- .../GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift | 2 +- DemoApps/GRDBDemoiOS/GRDBDemoiOS/Player.swift | 8 +++++++- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDatabase.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDatabase.swift index dd39c279ed..54ebb3e45a 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDatabase.swift +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDatabase.swift @@ -8,11 +8,10 @@ struct AppDatabase { /// Creates a fully initialized database at path static func openDatabase(atPath path: String) throws -> DatabaseQueue { // Connect to the database - // See https://github.com/groue/GRDB.swift/#database-connections - dbQueue = try DatabaseQueue(path: path) + // See https://github.com/groue/GRDB.swift/blob/master/README.md#database-connections + let dbQueue = try DatabaseQueue(path: path) - // Use DatabaseMigrator to define the database schema - // See https://github.com/groue/GRDB.swift/#migrations + // Define the database schema try migrator.migrate(dbQueue) return dbQueue @@ -20,8 +19,7 @@ struct AppDatabase { /// The DatabaseMigrator that defines the database schema. /// - /// This migrator is exposed so that migrations can be tested. - // See https://github.com/groue/GRDB.swift/#migrations + /// See https://github.com/groue/GRDB.swift/blob/master/README.md#migrations static var migrator: DatabaseMigrator { var migrator = DatabaseMigrator() @@ -29,11 +27,10 @@ struct AppDatabase { // Create a table // See https://github.com/groue/GRDB.swift#create-tables try db.create(table: "player") { t in - // An integer primary key auto-generates unique IDs - t.column("id", .integer).primaryKey() + t.autoIncrementedPrimaryKey("id") // Sort player names in a localized case insensitive fashion by default - // See https://github.com/groue/GRDB.swift/#unicode + // See https://github.com/groue/GRDB.swift/blob/master/README.md#unicode t.column("name", .text).notNull().collate(.localizedCaseInsensitiveCompare) t.column("score", .integer).notNull() diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift index e88354b7a1..672c383c03 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift @@ -20,7 +20,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { dbQueue = try AppDatabase.openDatabase(atPath: databaseURL.path) // Be a nice iOS citizen, and don't consume too much memory - // See https://github.com/groue/GRDB.swift/#memory-management + // See https://github.com/groue/GRDB.swift/blob/master/README.md#memory-management dbQueue.setupMemoryManagement(in: application) } } diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Player.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Player.swift index 66094ceabe..3d22cef065 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Player.swift +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Player.swift @@ -1,13 +1,17 @@ import GRDB +// A plain Player struct struct Player { - var id: Int64? // Auto-incremented database ids are Int64 + // Prefer Int64 for auto-incremented database ids + var id: Int64? var name: String var score: Int } // MARK: - Persistence +// Turn Player into a Codable Record. +// See https://github.com/groue/GRDB.swift/blob/master/README.md#records extension Player: Codable, FetchableRecord, MutablePersistableRecord { // Add ColumnExpression to Codable's CodingKeys so that we can use them // as database columns. @@ -26,6 +30,8 @@ extension Player: Codable, FetchableRecord, MutablePersistableRecord { // MARK: - Database access +// Define some useful player requests. +// See https://github.com/groue/GRDB.swift/blob/master/README.md#requests extension Player { static func orderedByName() -> QueryInterfaceRequest { return Player.order(CodingKeys.name)