From cc346816d6c82f37572df9b15dd9a7bbe1f303af Mon Sep 17 00:00:00 2001 From: John Estropia Date: Wed, 5 Dec 2018 16:15:20 +0900 Subject: [PATCH] Added new error for cases when addStorageAndWait() is used with .allowSynchronousLightweightMigration but migrations are only allowed asynchronously (related to #277) --- Sources/CSError.swift | 11 ++++++++ ...reStore+CustomDebugStringConvertible.swift | 4 +++ Sources/CoreStoreError.swift | 27 +++++++++++++++++++ Sources/DataStack.swift | 12 +++++++++ 4 files changed, 54 insertions(+) diff --git a/Sources/CSError.swift b/Sources/CSError.swift index cec4792..9fb1feb 100644 --- a/Sources/CSError.swift +++ b/Sources/CSError.swift @@ -217,6 +217,17 @@ extension CoreStoreError: CoreStoreSwiftType, _ObjectiveCBridgeableError { return } self = .progressiveMigrationRequired(localStoreURL: localStoreURL) + + case .asynchronousMigrationRequired: + guard + let localStoreURL = info["localStoreURL"] as? URL, + case let nsError as NSError = info["NSError"] + else { + + self = .unknown + return + } + self = .asynchronousMigrationRequired(localStoreURL: localStoreURL, NSError: nsError) case .internalError: guard case let nsError as NSError = info["NSError"] else { diff --git a/Sources/CoreStore+CustomDebugStringConvertible.swift b/Sources/CoreStore+CustomDebugStringConvertible.swift index 0ff72f5..19b37d2 100644 --- a/Sources/CoreStore+CustomDebugStringConvertible.swift +++ b/Sources/CoreStore+CustomDebugStringConvertible.swift @@ -137,6 +137,10 @@ extension CoreStoreError: CustomDebugStringConvertible, CoreStoreDebugStringConv case .progressiveMigrationRequired(let localStoreURL): firstLine = ".progressiveMigrationRequired" info.append(("localStoreURL", localStoreURL)) + + case .asynchronousMigrationRequired(let localStoreURL): + firstLine = ".asynchronousMigrationRequired" + info.append(("localStoreURL", localStoreURL)) case .internalError(let NSError): firstLine = ".internalError" diff --git a/Sources/CoreStoreError.swift b/Sources/CoreStoreError.swift index 824dcd2..9dec792 100644 --- a/Sources/CoreStoreError.swift +++ b/Sources/CoreStoreError.swift @@ -53,6 +53,11 @@ public enum CoreStoreError: Error, CustomNSError, Hashable { Progressive migrations are disabled for a store, but an `NSMappingModel` could not be found for a specific source and destination model versions. */ case progressiveMigrationRequired(localStoreURL: URL) + + /** + The `LocalStorage` was configured with `.allowSynchronousLightweightMigration`, but the model can only be migrated asynchronously. + */ + case asynchronousMigrationRequired(localStoreURL: URL, NSError: NSError) /** An internal SDK call failed with the specified `NSError`. @@ -92,6 +97,9 @@ public enum CoreStoreError: Error, CustomNSError, Hashable { case .progressiveMigrationRequired: return CoreStoreErrorCode.progressiveMigrationRequired.rawValue + + case .asynchronousMigrationRequired: + return CoreStoreErrorCode.asynchronousMigrationRequired.rawValue case .internalError: return CoreStoreErrorCode.internalError.rawValue @@ -127,6 +135,12 @@ public enum CoreStoreError: Error, CustomNSError, Hashable { return [ "localStoreURL": localStoreURL ] + + case .asynchronousMigrationRequired(let localStoreURL, let nsError): + return [ + "localStoreURL": localStoreURL, + "NSError": nsError + ] case .internalError(let nsError): return [ @@ -161,6 +175,10 @@ public enum CoreStoreError: Error, CustomNSError, Hashable { case (.progressiveMigrationRequired(let url1), .progressiveMigrationRequired(let url2)): return url1 == url2 + + case (.asynchronousMigrationRequired(let url1, let NSError1), .asynchronousMigrationRequired(let url2, let NSError2)): + return url1 == url2 + && NSError1.isEqual(NSError2) case (.internalError(let NSError1), .internalError(let NSError2)): return NSError1.isEqual(NSError2) @@ -205,6 +223,10 @@ public enum CoreStoreError: Error, CustomNSError, Hashable { case .progressiveMigrationRequired(let localStoreURL): hasher.combine(localStoreURL) + case .asynchronousMigrationRequired(let localStoreURL, let nsError): + hasher.combine(localStoreURL) + hasher.combine(nsError) + case .internalError(let nsError): hasher.combine(nsError) @@ -261,6 +283,11 @@ public enum CoreStoreErrorCode: Int { Progressive migrations are disabled for a store, but an `NSMappingModel` could not be found for a specific source and destination model versions. */ case progressiveMigrationRequired + + /** + The `LocalStorage` was configured with `.allowSynchronousLightweightMigration`, but the model can only be migrated asynchronously. + */ + case asynchronousMigrationRequired /** An internal SDK call failed with the specified "NSError" userInfo key. diff --git a/Sources/DataStack.swift b/Sources/DataStack.swift index 8c2b058..78f274d 100644 --- a/Sources/DataStack.swift +++ b/Sources/DataStack.swift @@ -345,6 +345,18 @@ public final class DataStack: Equatable { ) return storage } + catch let error as NSError where storage.localStorageOptions.contains(.allowSynchronousLightweightMigration) && error.isCoreDataMigrationError { + + let storeError = CoreStoreError.asynchronousMigrationRequired( + localStoreURL: fileURL, + NSError: error + ) + CoreStore.log( + storeError, + "Failed to add \(cs_typeName(storage)) to the stack." + ) + throw storeError + } } catch {