Skip to content

Commit

Permalink
Tweaks to demo app
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Nov 17, 2018
1 parent ead2b13 commit f80f59c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
15 changes: 6 additions & 9 deletions DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,29 @@ 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
}

/// 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()

migrator.registerMigration("createPlayer") { db in
// 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()
Expand Down
2 changes: 1 addition & 1 deletion DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
8 changes: 7 additions & 1 deletion DemoApps/GRDBDemoiOS/GRDBDemoiOS/Player.swift
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<Player> {
return Player.order(CodingKeys.name)
Expand Down

0 comments on commit f80f59c

Please sign in to comment.