Skip to content

Commit

Permalink
Enhanced CoreData migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
lexrus committed Jan 20, 2015
1 parent 7c9efd8 commit 54ecce3
Showing 1 changed file with 45 additions and 30 deletions.
75 changes: 45 additions & 30 deletions VPNOnData/VPNDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,46 +69,25 @@ class VPNDataManager
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.dataDirectory.URLByAppendingPathComponent("VPNOn.sqlite")
var failureReason = "There was an error creating or loading the application's saved data."

let options = NSDictionary(
objects: [NSNumber(bool: true), NSNumber(bool: true), "WAL"],
forKeys: [NSMigratePersistentStoresAutomaticallyOption, NSInferMappingModelAutomaticallyOption, "journal_mode"])
self.migrateToVersion2(coordinator!)

// Migrate the old data
let oldURL = self._oldDataDirectory.URLByAppendingPathComponent("VPNOn.sqlite")
var error1: NSError? = nil
if let oldStore = coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: oldURL, options:options, error:&error1) {
if let err = error1 {
println("Failed to add old CoreData")
} else {
var migrationError: NSError?
let newStore = coordinator!.migratePersistentStore(oldStore, toURL: url, options: options, withType: NSSQLiteStoreType, error: &migrationError)
if let targetStore = newStore {
println("CoreData migrated!")
} else {
if let migrationErr = migrationError {
println("Failed to migrate CoreData: \(migrationErr)")
}
}
}
}
let url = self.dataDirectory.URLByAppendingPathComponent("VPNOn.sqlite")
var failureReason = "There was an error creating or loading the application's saved data."

var error2: NSError? = nil
if let newStore = coordinator!.persistentStoreForURL(url) {

} else if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options, error: &error2) == nil {
var error: NSError? = nil
if let store = coordinator!.persistentStoreForURL(url) { }
else if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
coordinator = nil
// Report any error we got.
let dict = NSMutableDictionary()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error2
error2 = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() 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.
NSLog("Unresolved error \(error2), \(error2!.userInfo)")
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}

Expand All @@ -126,6 +105,42 @@ class VPNDataManager
return managedObjectContext
}()

// MARK: - Migration

func migrateToVersion2(coordinator: NSPersistentStoreCoordinator) {
let srcURL = self._oldDataDirectory.URLByAppendingPathComponent("VPNOn.sqlite")
let dstURL = self.dataDirectory.URLByAppendingPathComponent("VPNOn.sqlite")

if !NSFileManager.defaultManager().fileExistsAtPath(srcURL.path!) {
return
}

if NSFileManager.defaultManager().fileExistsAtPath(dstURL.path!) {
return
}

let options = NSDictionary(
objects: [NSNumber(bool: true), NSNumber(bool: true), "WAL"],
forKeys: [NSMigratePersistentStoresAutomaticallyOption, NSInferMappingModelAutomaticallyOption, "journal_mode"])

var srcError: NSError?
if coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: srcURL, options: options, error: &srcError) == nil {
println("Failed to add src store: \(srcError)")
return
}

if let oldStore = coordinator.persistentStoreForURL(srcURL) {
var migrationError: NSError?
if coordinator.migratePersistentStore(oldStore, toURL: dstURL, options: options, withType: NSSQLiteStoreType, error: &migrationError) == nil {
println("Failed to migrate CoreData: \(migrationError)")
} else {
if NSFileManager.defaultManager().removeItemAtPath(srcURL.path!, error: nil) {
println("CoreData migration complete.")
}
}
}
}

// MARK: - Core Data Saving support

func saveContext () {
Expand Down

0 comments on commit 54ecce3

Please sign in to comment.