Skip to content

Commit 59e8e5d

Browse files
committed
Simplified extensions by reduces dependencies
1 parent 9885417 commit 59e8e5d

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

CoreDataMigration-Example/CoreData/Migration/CoreDataMigrationVersion.swift

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ enum CoreDataMigrationVersion: String, CaseIterable {
1818
// MARK: - Latest
1919

2020
static var latest: CoreDataMigrationVersion {
21-
return allCases.last!
21+
guard let latest = allCases.last else {
22+
fatalError("no model versions found")
23+
}
24+
25+
return latest
2226
}
2327

2428
// MARK: - Migration
@@ -35,16 +39,4 @@ enum CoreDataMigrationVersion: String, CaseIterable {
3539
return nil
3640
}
3741
}
38-
39-
// MARK: - Compatible
40-
41-
static func compatibleVersionForStoreMetadata(_ metadata: [String : Any]) -> CoreDataMigrationVersion? {
42-
let compatibleVersion = CoreDataMigrationVersion.allCases.first {
43-
let model = NSManagedObjectModel.managedObjectModel(forResource: $0.rawValue)
44-
45-
return model.isConfiguration(withName: nil, compatibleWithStoreMetadata: metadata)
46-
}
47-
48-
return compatibleVersion
49-
}
5042
}

CoreDataMigration-Example/CoreData/Migration/CoreDataMigrator.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,18 @@ class CoreDataMigrator: CoreDataMigratorProtocol {
114114
}
115115
}
116116
}
117+
118+
private extension CoreDataMigrationVersion {
119+
120+
// MARK: - Compatible
121+
122+
static func compatibleVersionForStoreMetadata(_ metadata: [String : Any]) -> CoreDataMigrationVersion? {
123+
let compatibleVersion = CoreDataMigrationVersion.allCases.first {
124+
let model = NSManagedObjectModel.managedObjectModel(forResource: $0.rawValue)
125+
126+
return model.isConfiguration(withName: nil, compatibleWithStoreMetadata: metadata)
127+
}
128+
129+
return compatibleVersion
130+
}
131+
}

CoreDataMigration-Example/Extensions/NSManagedObjectModel/NSManagedObjectModel+Compatible.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ extension NSManagedObjectModel {
1414
// MARK: - Compatible
1515

1616
static func compatibleModelForStoreMetadata(_ metadata: [String : Any]) -> NSManagedObjectModel? {
17-
guard let compatibleModelVersion = CoreDataMigrationVersion.compatibleVersionForStoreMetadata(metadata) else {
18-
return nil
19-
}
20-
21-
return NSManagedObjectModel.managedObjectModel(forResource: compatibleModelVersion.rawValue)
17+
let mainBundle = Bundle.main
18+
return NSManagedObjectModel.mergedModel(from: [mainBundle], forStoreMetadata: metadata)
2219
}
2320
}

0 commit comments

Comments
 (0)