From 662aaa1e759a41b990284a7f96d391972d7d9f4b Mon Sep 17 00:00:00 2001 From: John Rommel Estropia Date: Fri, 10 Nov 2017 02:48:37 +0900 Subject: [PATCH] force true lightweight migration --- Sources/DataStack+Migration.swift | 29 +++++++++++++++++-- .../NSPersistentStoreCoordinator+Setup.swift | 15 +++------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Sources/DataStack+Migration.swift b/Sources/DataStack+Migration.swift index 5de36bd..7c985f9 100644 --- a/Sources/DataStack+Migration.swift +++ b/Sources/DataStack+Migration.swift @@ -575,6 +575,7 @@ public extension DataStack { sourceModel: sourceModel, destinationModel: destinationModel, mappingModel: mappingModel, + migrationType: migrationType, progress: childProgress ) } @@ -680,10 +681,34 @@ public extension DataStack { return nil } - private func startMigrationForStorage(_ storage: T, sourceModel: NSManagedObjectModel, destinationModel: NSManagedObjectModel, mappingModel: NSMappingModel, progress: Progress) throws { + private func startMigrationForStorage(_ storage: T, sourceModel: NSManagedObjectModel, destinationModel: NSManagedObjectModel, mappingModel: NSMappingModel, migrationType: MigrationType, progress: Progress) throws { 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) .appendingPathComponent(Bundle.main.bundleIdentifier ?? "com.CoreStore.DataStack") .appendingPathComponent(ProcessInfo().globallyUniqueString) diff --git a/Sources/NSPersistentStoreCoordinator+Setup.swift b/Sources/NSPersistentStoreCoordinator+Setup.swift index 8bba039..13aa538 100644 --- a/Sources/NSPersistentStoreCoordinator+Setup.swift +++ b/Sources/NSPersistentStoreCoordinator+Setup.swift @@ -72,15 +72,13 @@ internal extension NSPersistentStoreCoordinator { } @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? - var storeError: NSError? - self.performSynchronously { + return try self.performSynchronously { do { - store = try self.addPersistentStore( + return try self.addPersistentStore( ofType: storeType, configurationName: configuration, at: storeURL, @@ -89,13 +87,8 @@ internal extension NSPersistentStoreCoordinator { } catch { - storeError = error as NSError + throw CoreStoreError(error) } } - if let store = store { - - return store - } - throw CoreStoreError(storeError) } }