Skip to content

Refactored project to simplify migration and support swift 4.2 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ language: objective-c
cache:
- bundler

osx_image: xcode9
osx_image: xcode10.1

before_install:
- bundle install

script:
- bundle exec fastlane run_tests --verbose
- bundle exec fastlane run_unit_tests --verbose
251 changes: 164 additions & 87 deletions CoreDataMigration-Example.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0830"
LastUpgradeVersion = "1010"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,8 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "NO">
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -49,14 +48,20 @@
ReferencedContainer = "container:CoreDataMigration-Example.xcodeproj">
</BuildableReference>
</MacroExpansion>
<EnvironmentVariables>
<EnvironmentVariable
key = "runningTests"
value = "YES"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
7 changes: 6 additions & 1 deletion CoreDataMigration-Example/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

// MARK: - AppLifecycle

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
guard ProcessInfo.processInfo.environment["runningTests"] == nil else {
FileManager.clearApplicationSupportDirectoryContents()
return true
}

CoreDataManager.shared.setup {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { // just for example purposes
Expand Down
15 changes: 0 additions & 15 deletions CoreDataMigration-Example/Application/main.swift

This file was deleted.

14 changes: 9 additions & 5 deletions CoreDataMigration-Example/CoreData/CoreDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import CoreData

class CoreDataManager {

let migrator: CoreDataMigrator
let migrator: CoreDataMigratorProtocol
private let storeType: String

lazy var persistentContainer: NSPersistentContainer! = {
lazy var persistentContainer: NSPersistentContainer = {
let persistentContainer = NSPersistentContainer(name: "CoreDataMigration_Example")
let description = persistentContainer.persistentStoreDescriptions.first
description?.shouldInferMappingModelAutomatically = false //inferred mapping will be handled else where
description?.shouldMigrateStoreAutomatically = false
description?.type = storeType

return persistentContainer
}()
Expand All @@ -41,7 +44,8 @@ class CoreDataManager {

// MARK: - Init

init(migrator: CoreDataMigrator = CoreDataMigrator()) {
init(storeType: String = NSSQLiteStoreType, migrator: CoreDataMigratorProtocol = CoreDataMigrator()) {
self.storeType = storeType
self.migrator = migrator
}

Expand Down Expand Up @@ -72,9 +76,9 @@ class CoreDataManager {
fatalError("persistentContainer was not set up properly")
}

if migrator.requiresMigration(at: storeURL) {
if migrator.requiresMigration(at: storeURL, toVersion: CoreDataMigrationVersion.latest) {
DispatchQueue.global(qos: .userInitiated).async {
self.migrator.migrateStore(at: storeURL)
self.migrator.migrateStore(at: storeURL, toVersion: CoreDataMigrationVersion.latest)

DispatchQueue.main.async {
completion()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,40 @@ import CoreData

struct CoreDataMigrationStep {

let source: NSManagedObjectModel
let destination: NSManagedObjectModel
let mapping: NSMappingModel
let sourceModel: NSManagedObjectModel
let destinationModel: NSManagedObjectModel
let mappingModel: NSMappingModel

// MARK: Init

init(sourceVersion: CoreDataMigrationVersion, destinationVersion: CoreDataMigrationVersion) {
let sourceModel = NSManagedObjectModel.managedObjectModel(forResource: sourceVersion.rawValue)
let destinationModel = NSManagedObjectModel.managedObjectModel(forResource: destinationVersion.rawValue)

guard let mappingModel = CoreDataMigrationStep.mappingModel(fromSourceModel: sourceModel, toDestinationModel: destinationModel) else {
fatalError("Expected modal mapping not present")
}

self.sourceModel = sourceModel
self.destinationModel = destinationModel
self.mappingModel = mappingModel
}

// MARK: - Mapping

private static func mappingModel(fromSourceModel sourceModel: NSManagedObjectModel, toDestinationModel destinationModel: NSManagedObjectModel) -> NSMappingModel? {
guard let customMapping = customMappingModel(fromSourceModel: sourceModel, toDestinationModel: destinationModel) else {
return inferredMappingModel(fromSourceModel:sourceModel, toDestinationModel: destinationModel)
}

return customMapping
}

private static func inferredMappingModel(fromSourceModel sourceModel: NSManagedObjectModel, toDestinationModel destinationModel: NSManagedObjectModel) -> NSMappingModel? {
return try? NSMappingModel.inferredMappingModel(forSourceModel: sourceModel, destinationModel: destinationModel)
}

private static func customMappingModel(fromSourceModel sourceModel: NSManagedObjectModel, toDestinationModel destinationModel: NSManagedObjectModel) -> NSMappingModel? {
return NSMappingModel(from: [Bundle.main], forSourceModel: sourceModel, destinationModel: destinationModel)
}
}
Loading