From 3d21c254e512c3a87e0732c6b34a260d09795124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 3 Oct 2015 15:21:04 +0200 Subject: [PATCH] Improve GRDBDemoiOS --- .../GRDBDemoiOS.xcodeproj/project.pbxproj | 270 ++++-------------- .../GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift | 35 ++- .../Base.lproj/LaunchScreen.storyboard | 4 +- .../GRDBDemoiOS/Base.lproj/Main.storyboard | 146 +++++++++- .../GRDBDemoiOS/DetailViewController.swift | 16 +- .../GRDBDemoiOS/MasterViewController.swift | 76 +++-- .../PersonEditionViewController.swift | 61 ++++ .../GRDBDemoiOSTests/GRDBDemoiOSTests.swift | 27 -- .../GRDBDemoiOS/GRDBDemoiOSTests/Info.plist | 24 -- .../GRDBDemoiOSUITests.swift | 27 -- .../GRDBDemoiOS/GRDBDemoiOSUITests/Info.plist | 24 -- DemoApps/GRDBDemoiOS/README.md | 24 ++ README.md | 12 +- 13 files changed, 365 insertions(+), 381 deletions(-) create mode 100644 DemoApps/GRDBDemoiOS/GRDBDemoiOS/PersonEditionViewController.swift delete mode 100644 DemoApps/GRDBDemoiOS/GRDBDemoiOSTests/GRDBDemoiOSTests.swift delete mode 100644 DemoApps/GRDBDemoiOS/GRDBDemoiOSTests/Info.plist delete mode 100644 DemoApps/GRDBDemoiOS/GRDBDemoiOSUITests/GRDBDemoiOSUITests.swift delete mode 100644 DemoApps/GRDBDemoiOS/GRDBDemoiOSUITests/Info.plist create mode 100644 DemoApps/GRDBDemoiOS/README.md diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS.xcodeproj/project.pbxproj b/DemoApps/GRDBDemoiOS/GRDBDemoiOS.xcodeproj/project.pbxproj index f60fed832..76a3202fe 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS.xcodeproj/project.pbxproj +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS.xcodeproj/project.pbxproj @@ -7,14 +7,13 @@ objects = { /* Begin PBXBuildFile section */ + 56900E421BBFF7D500B815B5 /* PersonEditionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56900E411BBFF7D500B815B5 /* PersonEditionViewController.swift */; settings = {ASSET_TAGS = (); }; }; 56B8F4581B4E22B200C24296 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B8F4571B4E22B200C24296 /* AppDelegate.swift */; }; 56B8F45A1B4E22B200C24296 /* MasterViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B8F4591B4E22B200C24296 /* MasterViewController.swift */; }; 56B8F45C1B4E22B200C24296 /* DetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B8F45B1B4E22B200C24296 /* DetailViewController.swift */; }; 56B8F45F1B4E22B200C24296 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 56B8F45D1B4E22B200C24296 /* Main.storyboard */; }; 56B8F4611B4E22B200C24296 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 56B8F4601B4E22B200C24296 /* Assets.xcassets */; }; 56B8F4641B4E22B200C24296 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 56B8F4621B4E22B200C24296 /* LaunchScreen.storyboard */; }; - 56B8F46F1B4E22B200C24296 /* GRDBDemoiOSTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B8F46E1B4E22B200C24296 /* GRDBDemoiOSTests.swift */; }; - 56B8F47A1B4E22B200C24296 /* GRDBDemoiOSUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B8F4791B4E22B200C24296 /* GRDBDemoiOSUITests.swift */; }; 56B8F4991B4E23AA00C24296 /* Person.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B8F4981B4E23AA00C24296 /* Person.swift */; }; /* End PBXBuildFile section */ @@ -54,23 +53,10 @@ remoteGlobalIDString = 56E5D7C91B4D3FED00430942; remoteInfo = GRDBiOS; }; - 56B8F46B1B4E22B200C24296 /* PBXContainerItemProxy */ = { - isa = PBXContainerItemProxy; - containerPortal = 56B8F44C1B4E22B200C24296 /* Project object */; - proxyType = 1; - remoteGlobalIDString = 56B8F4531B4E22B200C24296; - remoteInfo = GRDBDemoiOS; - }; - 56B8F4761B4E22B200C24296 /* PBXContainerItemProxy */ = { - isa = PBXContainerItemProxy; - containerPortal = 56B8F44C1B4E22B200C24296 /* Project object */; - proxyType = 1; - remoteGlobalIDString = 56B8F4531B4E22B200C24296; - remoteInfo = GRDBDemoiOS; - }; /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 56900E411BBFF7D500B815B5 /* PersonEditionViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PersonEditionViewController.swift; sourceTree = ""; }; 56B8F4541B4E22B200C24296 /* GRDBDemoiOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GRDBDemoiOS.app; sourceTree = BUILT_PRODUCTS_DIR; }; 56B8F4571B4E22B200C24296 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 56B8F4591B4E22B200C24296 /* MasterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MasterViewController.swift; sourceTree = ""; }; @@ -79,12 +65,6 @@ 56B8F4601B4E22B200C24296 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 56B8F4631B4E22B200C24296 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 56B8F4651B4E22B200C24296 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 56B8F46A1B4E22B200C24296 /* GRDBDemoiOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBDemoiOSTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; - 56B8F46E1B4E22B200C24296 /* GRDBDemoiOSTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GRDBDemoiOSTests.swift; sourceTree = ""; }; - 56B8F4701B4E22B200C24296 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 56B8F4751B4E22B200C24296 /* GRDBDemoiOSUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBDemoiOSUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; - 56B8F4791B4E22B200C24296 /* GRDBDemoiOSUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GRDBDemoiOSUITests.swift; sourceTree = ""; }; - 56B8F47B1B4E22B200C24296 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 56B8F4871B4E235C00C24296 /* GRDB.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GRDB.xcodeproj; path = ../../../GRDB.xcodeproj; sourceTree = ""; }; 56B8F4981B4E23AA00C24296 /* Person.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Person.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -97,20 +77,6 @@ ); runOnlyForDeploymentPostprocessing = 0; }; - 56B8F4671B4E22B200C24296 /* Frameworks */ = { - isa = PBXFrameworksBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 56B8F4721B4E22B200C24296 /* Frameworks */ = { - isa = PBXFrameworksBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ @@ -125,12 +91,54 @@ name = Products; sourceTree = ""; }; + 56900E3C1BBFF2C400B815B5 /* Resources */ = { + isa = PBXGroup; + children = ( + 56B8F4601B4E22B200C24296 /* Assets.xcassets */, + 56B8F4621B4E22B200C24296 /* LaunchScreen.storyboard */, + 56B8F45D1B4E22B200C24296 /* Main.storyboard */, + ); + name = Resources; + sourceTree = ""; + }; + 56900E3D1BBFF2DB00B815B5 /* Frameworks */ = { + isa = PBXGroup; + children = ( + 56B8F4871B4E235C00C24296 /* GRDB.xcodeproj */, + ); + name = Frameworks; + sourceTree = ""; + }; + 56900E3E1BBFF2E900B815B5 /* Controllers */ = { + isa = PBXGroup; + children = ( + 56B8F45B1B4E22B200C24296 /* DetailViewController.swift */, + 56B8F4591B4E22B200C24296 /* MasterViewController.swift */, + 56900E411BBFF7D500B815B5 /* PersonEditionViewController.swift */, + ); + name = Controllers; + sourceTree = ""; + }; + 56900E3F1BBFF2F200B815B5 /* Models */ = { + isa = PBXGroup; + children = ( + 56B8F4981B4E23AA00C24296 /* Person.swift */, + ); + name = Models; + sourceTree = ""; + }; + 56900E401BBFF31800B815B5 /* Supporting Files */ = { + isa = PBXGroup; + children = ( + 56B8F4651B4E22B200C24296 /* Info.plist */, + ); + name = "Supporting Files"; + sourceTree = ""; + }; 56B8F44B1B4E22B200C24296 = { isa = PBXGroup; children = ( 56B8F4561B4E22B200C24296 /* GRDBDemoiOS */, - 56B8F46D1B4E22B200C24296 /* GRDBDemoiOSTests */, - 56B8F4781B4E22B200C24296 /* GRDBDemoiOSUITests */, 56B8F4551B4E22B200C24296 /* Products */, ); sourceTree = ""; @@ -139,8 +147,6 @@ isa = PBXGroup; children = ( 56B8F4541B4E22B200C24296 /* GRDBDemoiOS.app */, - 56B8F46A1B4E22B200C24296 /* GRDBDemoiOSTests.xctest */, - 56B8F4751B4E22B200C24296 /* GRDBDemoiOSUITests.xctest */, ); name = Products; sourceTree = ""; @@ -148,37 +154,16 @@ 56B8F4561B4E22B200C24296 /* GRDBDemoiOS */ = { isa = PBXGroup; children = ( - 56B8F4871B4E235C00C24296 /* GRDB.xcodeproj */, 56B8F4571B4E22B200C24296 /* AppDelegate.swift */, - 56B8F4591B4E22B200C24296 /* MasterViewController.swift */, - 56B8F45B1B4E22B200C24296 /* DetailViewController.swift */, - 56B8F4981B4E23AA00C24296 /* Person.swift */, - 56B8F45D1B4E22B200C24296 /* Main.storyboard */, - 56B8F4601B4E22B200C24296 /* Assets.xcassets */, - 56B8F4621B4E22B200C24296 /* LaunchScreen.storyboard */, - 56B8F4651B4E22B200C24296 /* Info.plist */, + 56900E3E1BBFF2E900B815B5 /* Controllers */, + 56900E3F1BBFF2F200B815B5 /* Models */, + 56900E3C1BBFF2C400B815B5 /* Resources */, + 56900E3D1BBFF2DB00B815B5 /* Frameworks */, + 56900E401BBFF31800B815B5 /* Supporting Files */, ); path = GRDBDemoiOS; sourceTree = ""; }; - 56B8F46D1B4E22B200C24296 /* GRDBDemoiOSTests */ = { - isa = PBXGroup; - children = ( - 56B8F46E1B4E22B200C24296 /* GRDBDemoiOSTests.swift */, - 56B8F4701B4E22B200C24296 /* Info.plist */, - ); - path = GRDBDemoiOSTests; - sourceTree = ""; - }; - 56B8F4781B4E22B200C24296 /* GRDBDemoiOSUITests */ = { - isa = PBXGroup; - children = ( - 56B8F4791B4E22B200C24296 /* GRDBDemoiOSUITests.swift */, - 56B8F47B1B4E22B200C24296 /* Info.plist */, - ); - path = GRDBDemoiOSUITests; - sourceTree = ""; - }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -200,42 +185,6 @@ productReference = 56B8F4541B4E22B200C24296 /* GRDBDemoiOS.app */; productType = "com.apple.product-type.application"; }; - 56B8F4691B4E22B200C24296 /* GRDBDemoiOSTests */ = { - isa = PBXNativeTarget; - buildConfigurationList = 56B8F4811B4E22B200C24296 /* Build configuration list for PBXNativeTarget "GRDBDemoiOSTests" */; - buildPhases = ( - 56B8F4661B4E22B200C24296 /* Sources */, - 56B8F4671B4E22B200C24296 /* Frameworks */, - 56B8F4681B4E22B200C24296 /* Resources */, - ); - buildRules = ( - ); - dependencies = ( - 56B8F46C1B4E22B200C24296 /* PBXTargetDependency */, - ); - name = GRDBDemoiOSTests; - productName = GRDBDemoiOSTests; - productReference = 56B8F46A1B4E22B200C24296 /* GRDBDemoiOSTests.xctest */; - productType = "com.apple.product-type.bundle.unit-test"; - }; - 56B8F4741B4E22B200C24296 /* GRDBDemoiOSUITests */ = { - isa = PBXNativeTarget; - buildConfigurationList = 56B8F4841B4E22B200C24296 /* Build configuration list for PBXNativeTarget "GRDBDemoiOSUITests" */; - buildPhases = ( - 56B8F4711B4E22B200C24296 /* Sources */, - 56B8F4721B4E22B200C24296 /* Frameworks */, - 56B8F4731B4E22B200C24296 /* Resources */, - ); - buildRules = ( - ); - dependencies = ( - 56B8F4771B4E22B200C24296 /* PBXTargetDependency */, - ); - name = GRDBDemoiOSUITests; - productName = GRDBDemoiOSUITests; - productReference = 56B8F4751B4E22B200C24296 /* GRDBDemoiOSUITests.xctest */; - productType = "com.apple.product-type.bundle.ui-testing"; - }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ @@ -248,14 +197,6 @@ 56B8F4531B4E22B200C24296 = { CreatedOnToolsVersion = 7.0; }; - 56B8F4691B4E22B200C24296 = { - CreatedOnToolsVersion = 7.0; - TestTargetID = 56B8F4531B4E22B200C24296; - }; - 56B8F4741B4E22B200C24296 = { - CreatedOnToolsVersion = 7.0; - TestTargetID = 56B8F4531B4E22B200C24296; - }; }; }; buildConfigurationList = 56B8F44F1B4E22B200C24296 /* Build configuration list for PBXProject "GRDBDemoiOS" */; @@ -278,8 +219,6 @@ projectRoot = ""; targets = ( 56B8F4531B4E22B200C24296 /* GRDBDemoiOS */, - 56B8F4691B4E22B200C24296 /* GRDBDemoiOSTests */, - 56B8F4741B4E22B200C24296 /* GRDBDemoiOSUITests */, ); }; /* End PBXProject section */ @@ -326,20 +265,6 @@ ); runOnlyForDeploymentPostprocessing = 0; }; - 56B8F4681B4E22B200C24296 /* Resources */ = { - isa = PBXResourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 56B8F4731B4E22B200C24296 /* Resources */ = { - isa = PBXResourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ @@ -350,26 +275,11 @@ 56B8F45C1B4E22B200C24296 /* DetailViewController.swift in Sources */, 56B8F45A1B4E22B200C24296 /* MasterViewController.swift in Sources */, 56B8F4991B4E23AA00C24296 /* Person.swift in Sources */, + 56900E421BBFF7D500B815B5 /* PersonEditionViewController.swift in Sources */, 56B8F4581B4E22B200C24296 /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; - 56B8F4661B4E22B200C24296 /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - 56B8F46F1B4E22B200C24296 /* GRDBDemoiOSTests.swift in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 56B8F4711B4E22B200C24296 /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - 56B8F47A1B4E22B200C24296 /* GRDBDemoiOSUITests.swift in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ @@ -378,16 +288,6 @@ name = GRDBiOS; targetProxy = 567AA8AC1B4E41CF0082C3E4 /* PBXContainerItemProxy */; }; - 56B8F46C1B4E22B200C24296 /* PBXTargetDependency */ = { - isa = PBXTargetDependency; - target = 56B8F4531B4E22B200C24296 /* GRDBDemoiOS */; - targetProxy = 56B8F46B1B4E22B200C24296 /* PBXContainerItemProxy */; - }; - 56B8F4771B4E22B200C24296 /* PBXTargetDependency */ = { - isa = PBXTargetDependency; - target = 56B8F4531B4E22B200C24296 /* GRDBDemoiOS */; - targetProxy = 56B8F4761B4E22B200C24296 /* PBXContainerItemProxy */; - }; /* End PBXTargetDependency section */ /* Begin PBXVariantGroup section */ @@ -515,54 +415,6 @@ }; name = Release; }; - 56B8F4821B4E22B200C24296 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - BUNDLE_LOADER = "$(TEST_HOST)"; - INFOPLIST_FILE = GRDBDemoiOSTests/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = com.github.groue.GRDBDemoiOSTests; - PRODUCT_NAME = "$(TARGET_NAME)"; - TEST_HOST = "$(BUILT_PRODUCTS_DIR)/GRDBDemoiOS.app/GRDBDemoiOS"; - }; - name = Debug; - }; - 56B8F4831B4E22B200C24296 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - BUNDLE_LOADER = "$(TEST_HOST)"; - INFOPLIST_FILE = GRDBDemoiOSTests/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = com.github.groue.GRDBDemoiOSTests; - PRODUCT_NAME = "$(TARGET_NAME)"; - TEST_HOST = "$(BUILT_PRODUCTS_DIR)/GRDBDemoiOS.app/GRDBDemoiOS"; - }; - name = Release; - }; - 56B8F4851B4E22B200C24296 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - INFOPLIST_FILE = GRDBDemoiOSUITests/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = com.github.groue.GRDBDemoiOSUITests; - PRODUCT_NAME = "$(TARGET_NAME)"; - TEST_TARGET_NAME = GRDBDemoiOS; - USES_XCTRUNNER = YES; - }; - name = Debug; - }; - 56B8F4861B4E22B200C24296 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - INFOPLIST_FILE = GRDBDemoiOSUITests/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = com.github.groue.GRDBDemoiOSUITests; - PRODUCT_NAME = "$(TARGET_NAME)"; - TEST_TARGET_NAME = GRDBDemoiOS; - USES_XCTRUNNER = YES; - }; - name = Release; - }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ @@ -584,24 +436,6 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; - 56B8F4811B4E22B200C24296 /* Build configuration list for PBXNativeTarget "GRDBDemoiOSTests" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 56B8F4821B4E22B200C24296 /* Debug */, - 56B8F4831B4E22B200C24296 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - 56B8F4841B4E22B200C24296 /* Build configuration list for PBXNativeTarget "GRDBDemoiOSUITests" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 56B8F4851B4E22B200C24296 /* Debug */, - 56B8F4861B4E22B200C24296 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; /* End XCConfigurationList section */ }; rootObject = 56B8F44C1B4E22B200C24296 /* Project object */; diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift index e5a5d295b..32202491c 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift @@ -1,26 +1,33 @@ import UIKit import GRDB +// The shared database queue, stored in a global. +// It is created in AppDelegate.application(_:didFinishLaunchingWithOptions:) var dbQueue: DatabaseQueue! @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate { - var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { - // Override point for customization after application launch. - let splitViewController = self.window!.rootViewController as! UISplitViewController - let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController - navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem() - splitViewController.delegate = self - // Database - dbQueue = DatabaseQueue() + // Connect to the database + + let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! as NSString + let databasePath = documentsPath.stringByAppendingPathComponent("db.sqlite") + dbQueue = try! DatabaseQueue(path: databasePath) + + + // Use DatabaseMigrator to setup the database + var migrator = DatabaseMigrator() migrator.registerMigration("createPersons") { db in - try db.execute("CREATE TABLE persons (id INTEGER PRIMARY KEY, firstName TEXT, lastName TEXT)") + try db.execute( + "CREATE TABLE persons (" + + "id INTEGER PRIMARY KEY, " + + "firstName TEXT, " + + "lastName TEXT " + + ")") } migrator.registerMigration("addPersons") { db in try Person(firstName: "Arthur", lastName: "Miller").insert(db) @@ -29,6 +36,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele } try! migrator.migrate(dbQueue) + + // Setup view controllers + + let splitViewController = self.window!.rootViewController as! UISplitViewController + let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController + navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem() + splitViewController.delegate = self + return true } diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Base.lproj/LaunchScreen.storyboard b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Base.lproj/LaunchScreen.storyboard index 93d3840bb..4b9623a74 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Base.lproj/LaunchScreen.storyboard +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Base.lproj/LaunchScreen.storyboard @@ -1,7 +1,7 @@ - + - + diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Base.lproj/Main.storyboard b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Base.lproj/Main.storyboard index b3b5a7877..fcfc8c10c 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Base.lproj/Main.storyboard +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Base.lproj/Main.storyboard @@ -1,7 +1,7 @@ - + - + @@ -22,7 +22,7 @@ - + @@ -56,6 +56,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -73,21 +191,21 @@ - + - + - + - + @@ -107,7 +225,15 @@ - + + + + + + + + + @@ -130,6 +256,6 @@ - + diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/DetailViewController.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/DetailViewController.swift index cd95088e8..d4bb4089c 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/DetailViewController.swift +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/DetailViewController.swift @@ -1,26 +1,24 @@ import UIKit class DetailViewController: UIViewController { - @IBOutlet weak var detailDescriptionLabel: UILabel! - var person: Person? { + var person: Person! { didSet { self.configureView() } } func configureView() { - if let person = self.person { - if let label = self.detailDescriptionLabel { - label.text = person.fullName - } + guard isViewLoaded() else { + return } + detailDescriptionLabel.text = person.fullName } - override func viewDidLoad() { - super.viewDidLoad() - self.configureView() + override func viewWillAppear(animated: Bool) { + super.viewWillAppear(animated) + configureView() } } diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/MasterViewController.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/MasterViewController.swift index ac61f4f3f..d9e5e8cf3 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/MasterViewController.swift +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/MasterViewController.swift @@ -1,16 +1,12 @@ import UIKit class MasterViewController: UITableViewController { - var detailViewController: DetailViewController? = nil var persons = [Person]() - override func viewDidLoad() { super.viewDidLoad() - // Do any additional setup after loading the view, typically from a nib. self.navigationItem.leftBarButtonItem = self.editButtonItem() - if let split = self.splitViewController { let controllers = split.viewControllers self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController @@ -18,33 +14,60 @@ class MasterViewController: UITableViewController { } override func viewWillAppear(animated: Bool) { - persons = dbQueue.inDatabase { db in - Person.fetchAll(db, "SELECT * FROM persons ORDER BY firstName, lastName") - } + // Reload persons, and the tableView + reloadPersons() + tableView.reloadData() + self.clearsSelectionOnViewWillAppear = self.splitViewController!.collapsed super.viewWillAppear(animated) } - - // MARK: - Segues - - override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { - if segue.identifier == "showDetail" { - if let indexPath = self.tableView.indexPathForSelectedRow { - let person = persons[indexPath.row] - let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController - controller.person = person - controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem() - controller.navigationItem.leftItemsSupplementBackButton = true - } + + func reloadPersons() { + persons = dbQueue.inDatabase { db in + Person.fetchAll(db, "SELECT * FROM persons ORDER BY LOWER(firstName), LOWER(lastName)") } } - - // MARK: - Table View - - override func numberOfSectionsInTableView(tableView: UITableView) -> Int { - return 1 + + + // MARK: - Segues + + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + if segue.identifier == "showPerson" { + let detailViewController = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController + detailViewController.person = persons[self.tableView.indexPathForSelectedRow!.row] + detailViewController.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem() + detailViewController.navigationItem.leftItemsSupplementBackButton = true + } + else if segue.identifier == "editNewPerson" { + let personEditionViewController = (segue.destinationViewController as! UINavigationController).topViewController as! PersonEditionViewController + personEditionViewController.person = Person() + } } - + + // Unwind action: commit person edition + @IBAction func commitPersonEdition(segue: UIStoryboardSegue) { + let personEditionViewController = segue.sourceViewController as! PersonEditionViewController + let person = personEditionViewController.person + + // Ignore person with no name + guard (person.firstName ?? "").characters.count > 0 || (person.lastName ?? "").characters.count > 0 else { + return + } + + // Save person + try! dbQueue.inDatabase { db in + try person.save(db) + } + + // Reload persons, and insert a cell. + reloadPersons() + let index = persons.indexOf { $0.id == person.id }! + tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: index, inSection: 0)], withRowAnimation: .Automatic) + } + + + // MARK: - Table View + override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return persons.count } @@ -58,13 +81,14 @@ class MasterViewController: UITableViewController { } override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { + // Delete the person let person = persons[indexPath.row] try! dbQueue.inTransaction { db in try person.delete(db) return .Commit } persons.removeAtIndex(indexPath.row) - tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) + tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) } diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/PersonEditionViewController.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/PersonEditionViewController.swift new file mode 100644 index 000000000..2d45d20af --- /dev/null +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/PersonEditionViewController.swift @@ -0,0 +1,61 @@ +import UIKit + +class PersonEditionViewController: UITableViewController, UITextFieldDelegate { + @IBOutlet weak var firstNameTableViewCell: UITableViewCell! + @IBOutlet weak var firstNameTextField: UITextField! + @IBOutlet weak var lastNameTableViewCell: UITableViewCell! + @IBOutlet weak var lastNameTextField: UITextField! + + // The edited person + var person: Person! + + override func viewDidLoad() { + super.viewDidLoad() + + // Initializes textFields with the person's names + firstNameTextField.text = person.firstName + lastNameTextField.text = person.lastName + } + + override func viewWillAppear(animated: Bool) { + super.viewWillAppear(animated) + + // Auto focus firstName + firstNameTextField.becomeFirstResponder() + } + + override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { + // Tapping a cell selects its text fields. + if indexPath == tableView.indexPathForCell(firstNameTableViewCell) { + firstNameTextField.becomeFirstResponder() + } else if indexPath == tableView.indexPathForCell(lastNameTableViewCell) { + lastNameTextField.becomeFirstResponder() + } + } + + func textFieldShouldReturn(textField: UITextField) -> Bool { + // Tapping Return key focuses next text field. + if textField == firstNameTextField { + lastNameTextField.becomeFirstResponder() + } else { + textField.resignFirstResponder() + } + return false + } + + func textFieldDidEndEditing(textField: UITextField) { + // Update person when text field ends editing. + if textField == firstNameTextField { + person.firstName = textField.text + } else if textField == lastNameTextField { + person.lastName = textField.text + } + } + + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + // About to cancel or commit: end editing. + // This dismisses the keyboard early, and apply pending changes through + // textFieldDidEndEditing(_). + view.endEditing(true) + } +} diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOSTests/GRDBDemoiOSTests.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOSTests/GRDBDemoiOSTests.swift deleted file mode 100644 index de04fc582..000000000 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOSTests/GRDBDemoiOSTests.swift +++ /dev/null @@ -1,27 +0,0 @@ -import XCTest - -class GRDBDemoiOSTests: XCTestCase { - - override func setUp() { - super.setUp() - // Put setup code here. This method is called before the invocation of each test method in the class. - } - - override func tearDown() { - // Put teardown code here. This method is called after the invocation of each test method in the class. - super.tearDown() - } - - func testExample() { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct results. - } - - func testPerformanceExample() { - // This is an example of a performance test case. - self.measureBlock() { - // Put the code you want to measure the time of here. - } - } - -} diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOSTests/Info.plist b/DemoApps/GRDBDemoiOS/GRDBDemoiOSTests/Info.plist deleted file mode 100644 index ba72822e8..000000000 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOSTests/Info.plist +++ /dev/null @@ -1,24 +0,0 @@ - - - - - CFBundleDevelopmentRegion - en - CFBundleExecutable - $(EXECUTABLE_NAME) - CFBundleIdentifier - $(PRODUCT_BUNDLE_IDENTIFIER) - CFBundleInfoDictionaryVersion - 6.0 - CFBundleName - $(PRODUCT_NAME) - CFBundlePackageType - BNDL - CFBundleShortVersionString - 1.0 - CFBundleSignature - ???? - CFBundleVersion - 1 - - diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOSUITests/GRDBDemoiOSUITests.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOSUITests/GRDBDemoiOSUITests.swift deleted file mode 100644 index 45235cc3e..000000000 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOSUITests/GRDBDemoiOSUITests.swift +++ /dev/null @@ -1,27 +0,0 @@ -import Foundation -import XCTest - -class GRDBDemoiOSUITests: XCTestCase { - - override func setUp() { - super.setUp() - - // Put setup code here. This method is called before the invocation of each test method in the class. - - // In UI tests it is usually best to stop immediately when a failure occurs. - continueAfterFailure = false - // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. - XCUIApplication().launch() - } - - override func tearDown() { - // Put teardown code here. This method is called after the invocation of each test method in the class. - super.tearDown() - } - - func testExample() { - // Use recording to get started writing UI tests. - // Use XCTAssert and related functions to verify your tests produce the correct results. - } - -} diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOSUITests/Info.plist b/DemoApps/GRDBDemoiOS/GRDBDemoiOSUITests/Info.plist deleted file mode 100644 index ba72822e8..000000000 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOSUITests/Info.plist +++ /dev/null @@ -1,24 +0,0 @@ - - - - - CFBundleDevelopmentRegion - en - CFBundleExecutable - $(EXECUTABLE_NAME) - CFBundleIdentifier - $(PRODUCT_BUNDLE_IDENTIFIER) - CFBundleInfoDictionaryVersion - 6.0 - CFBundleName - $(PRODUCT_NAME) - CFBundlePackageType - BNDL - CFBundleShortVersionString - 1.0 - CFBundleSignature - ???? - CFBundleVersion - 1 - - diff --git a/DemoApps/GRDBDemoiOS/README.md b/DemoApps/GRDBDemoiOS/README.md new file mode 100644 index 000000000..85ab01a95 --- /dev/null +++ b/DemoApps/GRDBDemoiOS/README.md @@ -0,0 +1,24 @@ +GRDBDemoiOS +=========== + +This application runs an universal (iPhone + iPad) application that displays an editable list of persons. + +To see usage of GRDB, check the following files: + +- [AppDelegate.swift](GRDBDemoiOS/AppDelegate.swift) + + AppDelegate sets up a global dbQueue that is accessible in the whole application. + + It uses migrations to initialize the database. + +- [MasterViewController.swift](GRDBDemoiOS/MasterViewController.swift) + + MasterViewController is responsible for displaying the list of persons in a table view, deleting persons, and present a person edition form. + +- [Person.swift](GRDBDemoiOS/Person.swift) + + Person is a simple Record subclass. + +- [DetailViewController.swift](GRDBDemoiOS/DetailViewController.swift) and [PersonEditionViewController.swift](GRDBDemoiOS/PersonEditionViewController.swift) + + Those two view controllers display and edit a Person, in total isolation from the database. diff --git a/README.md b/README.md index 0723e3f2b..bb3d4a076 100644 --- a/README.md +++ b/README.md @@ -133,10 +133,10 @@ To fiddle with the library, open the `GRDB.xcworkspace` workspace: it contains a **[Application Tools](#application-tools)** -- [Migrations](#migrations) -- [Database Changes Observation](#database-changes-observation) -- [RowConvertible Protocol](#rowconvertible-protocol) -- [Records](#records) +- [Migrations](#migrations): Transform your database as your application evolves. +- [Database Changes Observation](#database-changes-observation): A robust way to perform post-commit and post-rollback actions. +- [RowConvertible Protocol](#rowconvertible-protocol): Turn database rows into handy types, without sacrificing peformance. +- [Records](#records): CRUD operations and changes tracking. - [Core Methods](#core-methods) - [Fetching Records](#fetching-records) - [Tables and Primary Keys](#tables-and-primary-keys) @@ -145,6 +145,10 @@ To fiddle with the library, open the `GRDB.xcworkspace` workspace: it contains a - [Record Errors](#record-errors) - [Advice](#advice) +**Sample Code** + +- [GRDBDemoiOS](DemoApps/GRDBDemoiOS): A sample iOS application. + SQLite API ==========