Skip to content

Commit eb78d57

Browse files
committed
Refactored project to simplify migration and support swift 4.2
1 parent 2e0195b commit eb78d57

35 files changed

+767
-788
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ language: objective-c
33
cache:
44
- bundler
55

6-
osx_image: xcode9
6+
osx_image: xcode10.1
77

88
before_install:
99
- bundle install
1010

1111
script:
12-
- bundle exec fastlane run_tests --verbose
12+
- bundle exec fastlane run_unit_tests --verbose

CoreDataMigration-Example.xcodeproj/project.pbxproj

Lines changed: 124 additions & 67 deletions
Large diffs are not rendered by default.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

CoreDataMigration-Example.xcodeproj/xcshareddata/xcschemes/CoreDataMigration-Example.xcscheme

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0830"
3+
LastUpgradeVersion = "1010"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -26,7 +26,6 @@
2626
buildConfiguration = "Debug"
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29-
language = ""
3029
shouldUseLaunchSchemeArgsEnv = "YES">
3130
<Testables>
3231
<TestableReference
@@ -56,7 +55,6 @@
5655
buildConfiguration = "Debug"
5756
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
5857
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
59-
language = ""
6058
launchStyle = "0"
6159
useCustomWorkingDirectory = "NO"
6260
ignoresPersistentStateOnLaunch = "NO"

CoreDataMigration-Example/Application/AppDelegate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
import UIKit
1010
import CoreData
1111

12+
@UIApplicationMain
1213
class AppDelegate: UIResponder, UIApplicationDelegate {
1314

1415
var window: UIWindow?
1516

1617
// MARK: - AppLifecycle
1718

18-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
19+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1920

2021
CoreDataManager.shared.setup {
2122
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { // just for example purposes

CoreDataMigration-Example/Application/main.swift

Lines changed: 0 additions & 15 deletions
This file was deleted.

CoreDataMigration-Example/CoreData/CoreDataManager.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import CoreData
1212
class CoreDataManager {
1313

1414
let migrator: CoreDataMigrator
15+
private let storeType: String
1516

1617
lazy var persistentContainer: NSPersistentContainer! = {
1718
let persistentContainer = NSPersistentContainer(name: "CoreDataMigration_Example")
1819
let description = persistentContainer.persistentStoreDescriptions.first
1920
description?.shouldInferMappingModelAutomatically = false //inferred mapping will be handled else where
21+
description?.type = storeType
2022

2123
return persistentContainer
2224
}()
@@ -41,7 +43,8 @@ class CoreDataManager {
4143

4244
// MARK: - Init
4345

44-
init(migrator: CoreDataMigrator = CoreDataMigrator()) {
46+
init(storeType: String = NSSQLiteStoreType, migrator: CoreDataMigrator = CoreDataMigrator()) {
47+
self.storeType = storeType
4548
self.migrator = migrator
4649
}
4750

CoreDataMigration-Example/CoreData/Migration/CoreDataMigrationModel.swift

Lines changed: 0 additions & 207 deletions
This file was deleted.

CoreDataMigration-Example/CoreData/Migration/CoreDataMigrationStep.swift

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,40 @@ import CoreData
1010

1111
struct CoreDataMigrationStep {
1212

13-
let source: NSManagedObjectModel
14-
let destination: NSManagedObjectModel
15-
let mapping: NSMappingModel
13+
let sourceModel: NSManagedObjectModel
14+
let destinationModel: NSManagedObjectModel
15+
let mappingModel: NSMappingModel
16+
17+
// MARK: Init
18+
19+
init(sourceVersion: CoreDataMigrationVersion, destinationVersion: CoreDataMigrationVersion) {
20+
let sourceModel = NSManagedObjectModel.managedObjectModel(forResource: sourceVersion.rawValue)
21+
let destinationModel = NSManagedObjectModel.managedObjectModel(forResource: destinationVersion.rawValue)
22+
23+
guard let mappingModel = CoreDataMigrationStep.mappingModel(fromSourceModel: sourceModel, toDestinationModel: destinationModel) else {
24+
fatalError("Expected modal mapping not present")
25+
}
26+
27+
self.sourceModel = sourceModel
28+
self.destinationModel = destinationModel
29+
self.mappingModel = mappingModel
30+
}
31+
32+
// MARK: - Mapping
33+
34+
private static func mappingModel(fromSourceModel sourceModel: NSManagedObjectModel, toDestinationModel destinationModel: NSManagedObjectModel) -> NSMappingModel? {
35+
guard let customMapping = customMappingModel(fromSourceModel: sourceModel, toDestinationModel: destinationModel) else {
36+
return inferredMappingModel(fromSourceModel:sourceModel, toDestinationModel: destinationModel)
37+
}
38+
39+
return customMapping
40+
}
41+
42+
private static func inferredMappingModel(fromSourceModel sourceModel: NSManagedObjectModel, toDestinationModel destinationModel: NSManagedObjectModel) -> NSMappingModel? {
43+
return try? NSMappingModel.inferredMappingModel(forSourceModel: sourceModel, destinationModel: destinationModel)
44+
}
45+
46+
private static func customMappingModel(fromSourceModel sourceModel: NSManagedObjectModel, toDestinationModel destinationModel: NSManagedObjectModel) -> NSMappingModel? {
47+
return NSMappingModel(from: [Bundle.main], forSourceModel: sourceModel, destinationModel: destinationModel)
48+
}
1649
}

0 commit comments

Comments
 (0)