Skip to content

Commit d623a24

Browse files
committed
Extracted methods into extension
1 parent 6a4a23c commit d623a24

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

CoreDataMigration-Example/CoreData/CoreDataManager.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CoreDataManager {
5959
migrateStoreIfNeeded {
6060
self.persistentContainer.loadPersistentStores { description, error in
6161
guard error == nil else {
62-
fatalError("was unable to load store")
62+
fatalError("was unable to load store \(error!)")
6363
}
6464

6565
completion()
@@ -68,8 +68,8 @@ class CoreDataManager {
6868
}
6969

7070
private func migrateStoreIfNeeded(completion: @escaping () -> Void) {
71-
guard let description = persistentContainer.persistentStoreDescriptions.first, let storeURL = description.url else {
72-
fatalError("persistentContainer not set up properly")
71+
guard let storeURL = persistentContainer.persistentStoreDescriptions.first?.url else {
72+
fatalError("persistentContainer was not set up properly")
7373
}
7474

7575
if migrator.requiresMigration(storeURL: storeURL) {

CoreDataMigration-Example/CoreData/Migration/CoreDataMigrator.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,11 @@ class CoreDataMigrator {
9292
return
9393
}
9494

95-
do {
96-
let model = currentCoreDataStoreMigrationVersionModel.managedObjectModel()
97-
let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
98-
99-
let options = [NSSQLitePragmasOption: ["journal_mode": "DELETE"]]
100-
try persistentStoreCoordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeURL, options: options)
101-
try persistentStoreCoordinator.remove(persistentStoreCoordinator.persistentStore(for: storeURL)!)
102-
} catch let error {
103-
fatalError("failed when attempting to force WAL checkpointing, error: \(error)")
104-
}
95+
let model = currentCoreDataStoreMigrationVersionModel.managedObjectModel()
96+
let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
97+
98+
let options = [NSSQLitePragmasOption: ["journal_mode": "DELETE"]]
99+
persistentStoreCoordinator.addPersistentStore(at: storeURL, options: options)
100+
persistentStoreCoordinator.removeStore(at: storeURL)
105101
}
106102
}

CoreDataMigration-Example/Extensions/NSPersistentStoreCoordinator/NSPersistentStoreCoordinator+SQLite.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ extension NSPersistentStoreCoordinator {
2121
}
2222
}
2323

24+
// MARK: - Remove
25+
26+
func removeStore(at storeURL: URL) {
27+
guard let persistentStore = persistentStore(for: storeURL) else {
28+
return
29+
}
30+
31+
do {
32+
try remove(persistentStore)
33+
} catch let error {
34+
fatalError("failed to remove persistent store at \(storeURL), error: \(error)")
35+
}
36+
}
37+
2438
// MARK: - Replace
2539

2640
static func replaceStore(at targetURL: URL, withStoreAt sourceURL: URL) {
@@ -37,4 +51,15 @@ extension NSPersistentStoreCoordinator {
3751
static func metadata(at storeURL: URL) -> [String : Any]? {
3852
return try? NSPersistentStoreCoordinator.metadataForPersistentStore(ofType: NSSQLiteStoreType, at: storeURL, options: nil)
3953
}
54+
55+
// MARK: - Add
56+
57+
func addPersistentStore(at storeURL: URL, options: [AnyHashable : Any]) -> NSPersistentStore {
58+
do {
59+
return try addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeURL, options: options)
60+
} catch let error {
61+
fatalError("failed to add persistent store to coordinator, error: \(error)")
62+
}
63+
64+
}
4065
}

0 commit comments

Comments
 (0)