force true lightweight migration

This commit is contained in:
John Rommel Estropia
2017-11-10 02:48:37 +09:00
parent e314db8f56
commit d90e8d1303
2 changed files with 31 additions and 13 deletions

View File

@@ -575,6 +575,7 @@ public extension DataStack {
sourceModel: sourceModel, sourceModel: sourceModel,
destinationModel: destinationModel, destinationModel: destinationModel,
mappingModel: mappingModel, mappingModel: mappingModel,
migrationType: migrationType,
progress: childProgress progress: childProgress
) )
} }
@@ -679,9 +680,33 @@ public extension DataStack {
return nil return nil
} }
private func startMigrationForStorage<T: LocalStorage>(_ storage: T, sourceModel: NSManagedObjectModel, destinationModel: NSManagedObjectModel, mappingModel: NSMappingModel, progress: Progress) throws { private func startMigrationForStorage<T: LocalStorage>(_ storage: T, sourceModel: NSManagedObjectModel, destinationModel: NSManagedObjectModel, mappingModel: NSMappingModel, migrationType: MigrationType, progress: Progress) throws {
let fileURL = storage.fileURL let fileURL = storage.fileURL
if case .lightweight = migrationType {
do {
_ = try withExtendedLifetime(NSPersistentStoreCoordinator(managedObjectModel: destinationModel)) { (coordinator: NSPersistentStoreCoordinator) in
try coordinator.addPersistentStoreSynchronously(
type(of: storage).storeType,
configuration: storage.configuration,
URL: fileURL,
options: storage.dictionary(
forOptions: storage.localStorageOptions.union(.allowSynchronousLightweightMigration)
)
)
}
_ = try? storage.cs_finalizeStorageAndWait(soureModelHint: destinationModel)
progress.completedUnitCount = progress.totalUnitCount
return
}
catch {
// try manual migration
}
}
let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent(Bundle.main.bundleIdentifier ?? "com.CoreStore.DataStack") .appendingPathComponent(Bundle.main.bundleIdentifier ?? "com.CoreStore.DataStack")

View File

@@ -72,15 +72,13 @@ internal extension NSPersistentStoreCoordinator {
} }
@nonobjc @nonobjc
internal func addPersistentStoreSynchronously(_ storeType: String, configuration: ModelConfiguration, URL storeURL: URL?, options: [NSObject : AnyObject]?) throws -> NSPersistentStore { internal func addPersistentStoreSynchronously(_ storeType: String, configuration: ModelConfiguration, URL storeURL: URL?, options: [AnyHashable: Any]?) throws -> NSPersistentStore {
var store: NSPersistentStore? return try self.performSynchronously {
var storeError: NSError?
self.performSynchronously {
do { do {
store = try self.addPersistentStore( return try self.addPersistentStore(
ofType: storeType, ofType: storeType,
configurationName: configuration, configurationName: configuration,
at: storeURL, at: storeURL,
@@ -89,13 +87,8 @@ internal extension NSPersistentStoreCoordinator {
} }
catch { catch {
storeError = error as NSError throw CoreStoreError(error)
} }
} }
if let store = store {
return store
}
throw CoreStoreError(storeError)
} }
} }