Skip to content

Commit d1de81d

Browse files
authored
Refactor core data classes (#671)
1 parent ab45358 commit d1de81d

24 files changed

+598
-426
lines changed

pass.xcodeproj/project.pbxproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@
195195
DC4914961E434301007FF592 /* LabelTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC4914941E434301007FF592 /* LabelTableViewCell.swift */; };
196196
DC4914991E434600007FF592 /* PasswordDetailTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC4914981E434600007FF592 /* PasswordDetailTableViewController.swift */; };
197197
DC5F385B1E56AADB00C69ACA /* PGPKeyArmorImportTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC5F385A1E56AADB00C69ACA /* PGPKeyArmorImportTableViewController.swift */; };
198+
DC6474532D20DD0C004B4BBC /* CoreDataStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC6474522D20DD0C004B4BBC /* CoreDataStack.swift */; };
199+
DC64745C2D29BE9B004B4BBC /* PasswordEntityTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC6474592D29BD43004B4BBC /* PasswordEntityTest.swift */; };
200+
DC64745D2D29BEA9004B4BBC /* CoreDataTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC6474582D29BD43004B4BBC /* CoreDataTestCase.swift */; };
198201
DC7CBBBD2D0FA3F2003BB4D2 /* YubiKit in Frameworks */ = {isa = PBXBuildFile; productRef = DC7CBBBC2D0FA3F2003BB4D2 /* YubiKit */; };
199202
DC7CBBBF2D0FAC92003BB4D2 /* YKFSmartCardInterfaceExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC7CBBBE2D0FAC8E003BB4D2 /* YKFSmartCardInterfaceExtension.swift */; };
200203
DC8963C01E38EEB900828B09 /* SSHKeyURLImportTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC8963BF1E38EEB900828B09 /* SSHKeyURLImportTableViewController.swift */; };
@@ -493,6 +496,9 @@
493496
DC4914941E434301007FF592 /* LabelTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LabelTableViewCell.swift; sourceTree = "<group>"; };
494497
DC4914981E434600007FF592 /* PasswordDetailTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordDetailTableViewController.swift; sourceTree = "<group>"; };
495498
DC5F385A1E56AADB00C69ACA /* PGPKeyArmorImportTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PGPKeyArmorImportTableViewController.swift; sourceTree = "<group>"; };
499+
DC6474522D20DD0C004B4BBC /* CoreDataStack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataStack.swift; sourceTree = "<group>"; };
500+
DC6474582D29BD43004B4BBC /* CoreDataTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataTestCase.swift; sourceTree = "<group>"; };
501+
DC6474592D29BD43004B4BBC /* PasswordEntityTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PasswordEntityTest.swift; sourceTree = "<group>"; };
496502
DC7CBBBE2D0FAC8E003BB4D2 /* YKFSmartCardInterfaceExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YKFSmartCardInterfaceExtension.swift; sourceTree = "<group>"; };
497503
DC8963BF1E38EEB900828B09 /* SSHKeyURLImportTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SSHKeyURLImportTableViewController.swift; sourceTree = "<group>"; };
498504
DC917BD31E2E8231000FDF54 /* Pass.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Pass.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -868,6 +874,7 @@
868874
A26075861EEC6F34005DB03E /* passKitTests */ = {
869875
isa = PBXGroup;
870876
children = (
877+
DC64745A2D29BD43004B4BBC /* CoreData */,
871878
30A86F93230F235800F821A4 /* Crypto */,
872879
30BAC8C322E3BA4300438475 /* Testbase */,
873880
30697C5521F63F870064FCAC /* Extensions */,
@@ -901,6 +908,7 @@
901908
children = (
902909
30697C3121F63C8B0064FCAC /* PasscodeLockPresenter.swift */,
903910
30697C3221F63C8B0064FCAC /* PasscodeLockViewController.swift */,
911+
DC6474522D20DD0C004B4BBC /* CoreDataStack.swift */,
904912
);
905913
path = Controllers;
906914
sourceTree = "<group>";
@@ -1021,6 +1029,15 @@
10211029
path = Views;
10221030
sourceTree = "<group>";
10231031
};
1032+
DC64745A2D29BD43004B4BBC /* CoreData */ = {
1033+
isa = PBXGroup;
1034+
children = (
1035+
DC6474582D29BD43004B4BBC /* CoreDataTestCase.swift */,
1036+
DC6474592D29BD43004B4BBC /* PasswordEntityTest.swift */,
1037+
);
1038+
path = CoreData;
1039+
sourceTree = "<group>";
1040+
};
10241041
DC917BCA1E2E8231000FDF54 = {
10251042
isa = PBXGroup;
10261043
children = (
@@ -1589,6 +1606,7 @@
15891606
3087574F2343E42A00B971A2 /* Colors.swift in Sources */,
15901607
30697C2C21F63C5A0064FCAC /* FileManagerExtension.swift in Sources */,
15911608
30697C3321F63C8B0064FCAC /* PasscodeLockPresenter.swift in Sources */,
1609+
DC6474532D20DD0C004B4BBC /* CoreDataStack.swift in Sources */,
15921610
30697C3D21F63C990064FCAC /* UIViewExtension.swift in Sources */,
15931611
30697C3A21F63C990064FCAC /* UIViewControllerExtension.swift in Sources */,
15941612
30697C2E21F63C5A0064FCAC /* Utils.swift in Sources */,
@@ -1619,9 +1637,11 @@
16191637
30697C5F21F674800064FCAC /* String+UtilitiesTest.swift in Sources */,
16201638
3032328A22C9FBA2009EBD9C /* KeyFileManagerTest.swift in Sources */,
16211639
306623332406F1A8000E2AD6 /* PasswordGeneratorTest.swift in Sources */,
1640+
DC64745C2D29BE9B004B4BBC /* PasswordEntityTest.swift in Sources */,
16221641
30BAC8C722E3BAAF00438475 /* TestPGPKeys.swift in Sources */,
16231642
30A1D2AA21B32A0100E2D1F7 /* OTPTypeTest.swift in Sources */,
16241643
301F6468216165290071A4CE /* ConstantsTest.swift in Sources */,
1644+
DC64745D2D29BEA9004B4BBC /* CoreDataTestCase.swift in Sources */,
16251645
30A1D29C21AF451E00E2D1F7 /* PasswordGeneratorFlavorTest.swift in Sources */,
16261646
);
16271647
runOnlyForDeploymentPostprocessing = 0;

pass/AppDelegate.swift

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2323
lazy var passcodeLockPresenter = PasscodeLockPresenter(mainWindow: self.window)
2424

2525
func application(_: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
26+
PersistenceController.shared.setup()
2627
// Override point for customization after application launch.
2728
SVProgressHUD.setMinimumSize(CGSize(width: 150, height: 100))
2829
passcodeLockPresenter.present(windowLevel: UIApplication.shared.windows.last?.windowLevel.rawValue)
@@ -80,6 +81,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
8081
appIconView.center = (window?.center)!
8182
appIconView.tag = ViewTag.appicon.rawValue
8283
window?.addSubview(appIconView)
84+
85+
PersistenceController.shared.save()
8386
}
8487

8588
func applicationDidEnterBackground(_: UIApplication) {
@@ -102,55 +105,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
102105
func applicationWillTerminate(_: UIApplication) {
103106
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
104107
// Saves changes in the application's managed object context before the application terminates.
105-
saveContext()
106-
}
107-
108-
// MARK: - Core Data stack
109-
110-
lazy var persistentContainer: NSPersistentContainer = {
111-
// The persistent container for the application. This implementation
112-
// creates and returns a container, having loaded the store for the
113-
// application to it. This property is optional since there are legitimate
114-
// error conditions that could cause the creation of the store to fail.
115-
let modelURL = Bundle(identifier: Globals.passKitBundleIdentifier)!.url(forResource: "pass", withExtension: "momd")!
116-
let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL)
117-
let container = NSPersistentContainer(name: "pass", managedObjectModel: managedObjectModel!)
118-
if FileManager.default.fileExists(atPath: Globals.documentPath) {
119-
try! FileManager.default.createDirectory(atPath: Globals.documentPath, withIntermediateDirectories: true, attributes: nil)
120-
}
121-
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: URL(fileURLWithPath: Globals.dbPath))]
122-
container.loadPersistentStores { _, error in
123-
if let error = error as NSError? {
124-
// Replace this implementation with code to handle the error appropriately.
125-
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
126-
127-
// Typical reasons for an error here include:
128-
//
129-
// * The parent directory does not exist, cannot be created, or disallows writing.
130-
// * The persistent store is not accessible, due to permissions or data protection when the device is locked.
131-
// * The device is out of space.
132-
// * The store could not be migrated to the current model version.
133-
//
134-
// Check the error message to determine what the actual problem was.
135-
fatalError("UnresolvedError".localize("\(error), \(error.userInfo)"))
136-
}
137-
}
138-
return container
139-
}()
140-
141-
// MARK: - Core Data Saving support
142-
143-
func saveContext() {
144-
let context = persistentContainer.viewContext
145-
if context.hasChanges {
146-
do {
147-
try context.save()
148-
} catch {
149-
// Replace this implementation with code to handle the error appropriately.
150-
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
151-
let nserror = error as NSError
152-
fatalError("UnresolvedError".localize("\(nserror), \(nserror.userInfo)"))
153-
}
154-
}
108+
PersistenceController.shared.save()
155109
}
156110
}

0 commit comments

Comments
 (0)