Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

Commit 18f0574

Browse files
committed
Upgrade GRDB to v2.1.0, refactor application around a Codable record struct
1 parent 6f3c8de commit 18f0574

10 files changed

+262
-229
lines changed

GRDBDemo.xcodeproj/project.pbxproj

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
56B8C2611CABEE6B00510325 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 56B8C25F1CABEE6B00510325 /* Main.storyboard */; };
1212
56B8C2631CABEE6B00510325 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 56B8C2621CABEE6B00510325 /* Assets.xcassets */; };
1313
56B8C2661CABEE6B00510325 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 56B8C2641CABEE6B00510325 /* LaunchScreen.storyboard */; };
14-
56B8C2711CABEF1600510325 /* Database.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B8C26D1CABEF1600510325 /* Database.swift */; };
15-
56B8C2721CABEF1600510325 /* Person.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B8C26E1CABEF1600510325 /* Person.swift */; };
16-
56B8C2731CABEF1600510325 /* PersonEditionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B8C26F1CABEF1600510325 /* PersonEditionViewController.swift */; };
17-
56B8C2741CABEF1600510325 /* PersonsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B8C2701CABEF1600510325 /* PersonsViewController.swift */; };
14+
56B8C2711CABEF1600510325 /* AppDatabase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B8C26D1CABEF1600510325 /* AppDatabase.swift */; };
15+
56B8C2721CABEF1600510325 /* Player.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B8C26E1CABEF1600510325 /* Player.swift */; };
16+
56B8C2731CABEF1600510325 /* PlayerEditionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B8C26F1CABEF1600510325 /* PlayerEditionViewController.swift */; };
17+
56B8C2741CABEF1600510325 /* PlayersViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B8C2701CABEF1600510325 /* PlayersViewController.swift */; };
1818
D99804C2C5173B8409CD9EA8 /* Pods_GRDBDemo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 335AFD8AEC19ECD044D97DC9 /* Pods_GRDBDemo.framework */; };
1919
/* End PBXBuildFile section */
2020

@@ -27,10 +27,10 @@
2727
56B8C2621CABEE6B00510325 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
2828
56B8C2651CABEE6B00510325 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
2929
56B8C2671CABEE6B00510325 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
30-
56B8C26D1CABEF1600510325 /* Database.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Database.swift; sourceTree = "<group>"; };
31-
56B8C26E1CABEF1600510325 /* Person.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Person.swift; sourceTree = "<group>"; };
32-
56B8C26F1CABEF1600510325 /* PersonEditionViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PersonEditionViewController.swift; sourceTree = "<group>"; };
33-
56B8C2701CABEF1600510325 /* PersonsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PersonsViewController.swift; sourceTree = "<group>"; };
30+
56B8C26D1CABEF1600510325 /* AppDatabase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDatabase.swift; sourceTree = "<group>"; };
31+
56B8C26E1CABEF1600510325 /* Player.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Player.swift; sourceTree = "<group>"; };
32+
56B8C26F1CABEF1600510325 /* PlayerEditionViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlayerEditionViewController.swift; sourceTree = "<group>"; };
33+
56B8C2701CABEF1600510325 /* PlayersViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlayersViewController.swift; sourceTree = "<group>"; };
3434
9DF1761EF5816B02B81EB734 /* Pods.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3535
F3B356438E86E7509FF0B7C2 /* Pods-GRDBDemo.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GRDBDemo.debug.xcconfig"; path = "Pods/Target Support Files/Pods-GRDBDemo/Pods-GRDBDemo.debug.xcconfig"; sourceTree = "<group>"; };
3636
/* End PBXFileReference section */
@@ -69,10 +69,10 @@
6969
isa = PBXGroup;
7070
children = (
7171
56B8C25B1CABEE6B00510325 /* AppDelegate.swift */,
72-
56B8C26D1CABEF1600510325 /* Database.swift */,
73-
56B8C26E1CABEF1600510325 /* Person.swift */,
74-
56B8C26F1CABEF1600510325 /* PersonEditionViewController.swift */,
75-
56B8C2701CABEF1600510325 /* PersonsViewController.swift */,
72+
56B8C26D1CABEF1600510325 /* AppDatabase.swift */,
73+
56B8C26E1CABEF1600510325 /* Player.swift */,
74+
56B8C26F1CABEF1600510325 /* PlayerEditionViewController.swift */,
75+
56B8C2701CABEF1600510325 /* PlayersViewController.swift */,
7676
56B8C25F1CABEE6B00510325 /* Main.storyboard */,
7777
56B8C2621CABEE6B00510325 /* Assets.xcassets */,
7878
56B8C2641CABEE6B00510325 /* LaunchScreen.storyboard */,
@@ -129,10 +129,11 @@
129129
isa = PBXProject;
130130
attributes = {
131131
LastSwiftUpdateCheck = 0730;
132-
LastUpgradeCheck = 0830;
132+
LastUpgradeCheck = 0900;
133133
TargetAttributes = {
134134
56B8C2571CABEE6B00510325 = {
135135
CreatedOnToolsVersion = 7.3;
136+
DevelopmentTeam = AMD8W895CT;
136137
LastSwiftMigration = 0800;
137138
};
138139
};
@@ -175,13 +176,16 @@
175176
files = (
176177
);
177178
inputPaths = (
179+
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
180+
"${PODS_ROOT}/Manifest.lock",
178181
);
179182
name = "[CP] Check Pods Manifest.lock";
180183
outputPaths = (
184+
"$(DERIVED_FILE_DIR)/Pods-GRDBDemo-checkManifestLockResult.txt",
181185
);
182186
runOnlyForDeploymentPostprocessing = 0;
183187
shellPath = /bin/sh;
184-
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
188+
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
185189
showEnvVarsInLog = 0;
186190
};
187191
A4972E2ADB43A74A4024D29F /* [CP] Copy Pods Resources */ = {
@@ -205,9 +209,12 @@
205209
files = (
206210
);
207211
inputPaths = (
212+
"${SRCROOT}/Pods/Target Support Files/Pods-GRDBDemo/Pods-GRDBDemo-frameworks.sh",
213+
"${BUILT_PRODUCTS_DIR}/GRDB.swift/GRDB.framework",
208214
);
209215
name = "[CP] Embed Pods Frameworks";
210216
outputPaths = (
217+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GRDB.framework",
211218
);
212219
runOnlyForDeploymentPostprocessing = 0;
213220
shellPath = /bin/sh;
@@ -221,10 +228,10 @@
221228
isa = PBXSourcesBuildPhase;
222229
buildActionMask = 2147483647;
223230
files = (
224-
56B8C2721CABEF1600510325 /* Person.swift in Sources */,
225-
56B8C2711CABEF1600510325 /* Database.swift in Sources */,
226-
56B8C2731CABEF1600510325 /* PersonEditionViewController.swift in Sources */,
227-
56B8C2741CABEF1600510325 /* PersonsViewController.swift in Sources */,
231+
56B8C2721CABEF1600510325 /* Player.swift in Sources */,
232+
56B8C2711CABEF1600510325 /* AppDatabase.swift in Sources */,
233+
56B8C2731CABEF1600510325 /* PlayerEditionViewController.swift in Sources */,
234+
56B8C2741CABEF1600510325 /* PlayersViewController.swift in Sources */,
228235
56B8C25C1CABEE6B00510325 /* AppDelegate.swift in Sources */,
229236
);
230237
runOnlyForDeploymentPostprocessing = 0;
@@ -260,14 +267,20 @@
260267
CLANG_CXX_LIBRARY = "libc++";
261268
CLANG_ENABLE_MODULES = YES;
262269
CLANG_ENABLE_OBJC_ARC = YES;
270+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
263271
CLANG_WARN_BOOL_CONVERSION = YES;
272+
CLANG_WARN_COMMA = YES;
264273
CLANG_WARN_CONSTANT_CONVERSION = YES;
265274
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
266275
CLANG_WARN_EMPTY_BODY = YES;
267276
CLANG_WARN_ENUM_CONVERSION = YES;
268277
CLANG_WARN_INFINITE_RECURSION = YES;
269278
CLANG_WARN_INT_CONVERSION = YES;
279+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
280+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
270281
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
282+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
283+
CLANG_WARN_STRICT_PROTOTYPES = YES;
271284
CLANG_WARN_SUSPICIOUS_MOVE = YES;
272285
CLANG_WARN_UNREACHABLE_CODE = YES;
273286
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@@ -307,14 +320,20 @@
307320
CLANG_CXX_LIBRARY = "libc++";
308321
CLANG_ENABLE_MODULES = YES;
309322
CLANG_ENABLE_OBJC_ARC = YES;
323+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
310324
CLANG_WARN_BOOL_CONVERSION = YES;
325+
CLANG_WARN_COMMA = YES;
311326
CLANG_WARN_CONSTANT_CONVERSION = YES;
312327
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
313328
CLANG_WARN_EMPTY_BODY = YES;
314329
CLANG_WARN_ENUM_CONVERSION = YES;
315330
CLANG_WARN_INFINITE_RECURSION = YES;
316331
CLANG_WARN_INT_CONVERSION = YES;
332+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
333+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
317334
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
335+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
336+
CLANG_WARN_STRICT_PROTOTYPES = YES;
318337
CLANG_WARN_SUSPICIOUS_MOVE = YES;
319338
CLANG_WARN_UNREACHABLE_CODE = YES;
320339
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@@ -345,12 +364,13 @@
345364
buildSettings = {
346365
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = "$(inherited)";
347366
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
367+
DEVELOPMENT_TEAM = AMD8W895CT;
348368
INFOPLIST_FILE = GRDBDemo/Info.plist;
349369
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
350370
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
351-
PRODUCT_BUNDLE_IDENTIFIER = GRDB.GRDBDemo;
371+
PRODUCT_BUNDLE_IDENTIFIER = com.github.groue.GRDB.GRDBDemo;
352372
PRODUCT_NAME = "$(TARGET_NAME)";
353-
SWIFT_VERSION = 3.0;
373+
SWIFT_VERSION = 4.0;
354374
};
355375
name = Debug;
356376
};
@@ -360,12 +380,13 @@
360380
buildSettings = {
361381
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = "$(inherited)";
362382
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
383+
DEVELOPMENT_TEAM = AMD8W895CT;
363384
INFOPLIST_FILE = GRDBDemo/Info.plist;
364385
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
365386
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
366-
PRODUCT_BUNDLE_IDENTIFIER = GRDB.GRDBDemo;
387+
PRODUCT_BUNDLE_IDENTIFIER = com.github.groue.GRDB.GRDBDemo;
367388
PRODUCT_NAME = "$(TARGET_NAME)";
368-
SWIFT_VERSION = 3.0;
389+
SWIFT_VERSION = 4.0;
369390
};
370391
name = Release;
371392
};

GRDBDemo/AppDatabase.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import GRDB
2+
3+
/// A type responsible for initializing the application database.
4+
///
5+
/// See AppDelegate.setupDatabase()
6+
struct AppDatabase {
7+
8+
/// Creates a fully initialized database at path
9+
static func openDatabase(atPath path: String) throws -> DatabaseQueue {
10+
// Connect to the database
11+
// See https://github.com/groue/GRDB.swift/#database-connections
12+
dbQueue = try DatabaseQueue(path: path)
13+
14+
// Use DatabaseMigrator to define the database schema
15+
// See https://github.com/groue/GRDB.swift/#migrations
16+
try migrator.migrate(dbQueue)
17+
18+
return dbQueue
19+
}
20+
21+
/// The DatabaseMigrator that defines the database schema.
22+
///
23+
/// This migrator is exposed so that migrations can be tested.
24+
// See https://github.com/groue/GRDB.swift/#migrations
25+
static var migrator: DatabaseMigrator {
26+
var migrator = DatabaseMigrator()
27+
28+
migrator.registerMigration("createPlayers") { db in
29+
// Create a table
30+
// See https://github.com/groue/GRDB.swift#create-tables
31+
try db.create(table: "players") { t in
32+
// An integer primary key auto-generates unique IDs
33+
t.column("id", .integer).primaryKey()
34+
35+
// Sort player names in a localized case insensitive fashion by default
36+
// See https://github.com/groue/GRDB.swift/#unicode
37+
t.column("name", .text).notNull().collate(.localizedCaseInsensitiveCompare)
38+
39+
t.column("score", .integer).notNull()
40+
}
41+
}
42+
43+
migrator.registerMigration("fixtures") { db in
44+
// Populate the players table with random data
45+
for _ in 0..<8 {
46+
var player = Player(id: nil, name: Player.randomName(), score: Player.randomScore())
47+
try player.insert(db)
48+
}
49+
}
50+
51+
// // Migrations for future application versions will be inserted here:
52+
// migrator.registerMigration(...) { db in
53+
// ...
54+
// }
55+
56+
return migrator
57+
}
58+
}
59+

GRDBDemo/AppDelegate.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
import UIKit
2+
import GRDB
3+
4+
// The shared database queue
5+
var dbQueue: DatabaseQueue!
26

37
@UIApplicationMain
48
class AppDelegate: UIResponder, UIApplicationDelegate {
59
var window: UIWindow?
6-
7-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
10+
11+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
812
try! setupDatabase(application)
913
return true
1014
}
15+
16+
private func setupDatabase(_ application: UIApplication) throws {
17+
let databaseURL = try FileManager.default
18+
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
19+
.appendingPathComponent("db.sqlite")
20+
dbQueue = try AppDatabase.openDatabase(atPath: databaseURL.path)
21+
22+
// Be a nice iOS citizen, and don't consume too much memory
23+
// See https://github.com/groue/GRDB.swift/#memory-management
24+
dbQueue.setupMemoryManagement(in: application)
25+
}
1126
}

0 commit comments

Comments
 (0)