Added new error for cases when addStorageAndWait() is used with .allowSynchronousLightweightMigration but migrations are only allowed asynchronously (related to #277)

This commit is contained in:
John Estropia
2018-12-05 16:15:20 +09:00
parent 06c0981ded
commit cc346816d6
4 changed files with 54 additions and 0 deletions

View File

@@ -218,6 +218,17 @@ extension CoreStoreError: CoreStoreSwiftType, _ObjectiveCBridgeableError {
}
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 {

View File

@@ -138,6 +138,10 @@ extension CoreStoreError: CustomDebugStringConvertible, CoreStoreDebugStringConv
firstLine = ".progressiveMigrationRequired"
info.append(("localStoreURL", localStoreURL))
case .asynchronousMigrationRequired(let localStoreURL):
firstLine = ".asynchronousMigrationRequired"
info.append(("localStoreURL", localStoreURL))
case .internalError(let NSError):
firstLine = ".internalError"
info.append(("NSError", NSError))

View File

@@ -54,6 +54,11 @@ public enum CoreStoreError: Error, CustomNSError, Hashable {
*/
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`.
*/
@@ -93,6 +98,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
@@ -128,6 +136,12 @@ public enum CoreStoreError: Error, CustomNSError, Hashable {
"localStoreURL": localStoreURL
]
case .asynchronousMigrationRequired(let localStoreURL, let nsError):
return [
"localStoreURL": localStoreURL,
"NSError": nsError
]
case .internalError(let nsError):
return [
"NSError": nsError
@@ -162,6 +176,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)
@@ -262,6 +284,11 @@ public enum CoreStoreErrorCode: Int {
*/
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.
*/

View File

@@ -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 {