Changed error-handling method to rely on new enum CoreStoreError instead of NSErrors

This commit is contained in:
John Rommel Estropia
2016-03-16 07:56:19 +09:00
parent 245ec25ad8
commit d9422f7f2e
15 changed files with 296 additions and 179 deletions

View File

@@ -1,5 +1,5 @@
//
// NSError+CoreStore.swift
// CoreStoreError.swift
// CoreStore
//
// Copyright © 2014 John Rommel Estropia
@@ -29,14 +29,102 @@ import CoreData
// MARK: - CoreStoreError
public enum CoreStoreError: ErrorType, CustomStringConvertible, CustomDebugStringConvertible {
/**
A failure occured because of an unknown error.
*/
case Unknown
/**
The `NSPersistentStore` could note be initialized because another store existed at the specified `NSURL`.
*/
case DifferentStorageExistsAtURL(existingPersistentStoreURL: NSURL)
/**
An `NSMappingModel` could not be found for a specific source and destination model versions.
*/
case MappingModelNotFound(storage: LocalStorage, targetModel: NSManagedObjectModel, targetModelVersion: String)
/**
Progressive migrations are disabled for a store, but an `NSMappingModel` could not be found for a specific source and destination model versions.
*/
case ProgressiveMigrationRequired(storage: LocalStorage)
/**
An internal SDK call failed with the specified `NSError`.
*/
case InternalError(NSError)
// MARK: ErrorType
public var _domain: String {
return "com.corestore.error"
}
public var _code: Int {
switch self {
case .Unknown: return 1
case .DifferentStorageExistsAtURL: return 2
case .MappingModelNotFound: return 3
case .ProgressiveMigrationRequired: return 4
case .InternalError: return 5
}
}
// MARK: CustomStringConvertible
public var description: String {
// TODO:
return (self as NSError).description
}
// MARK: CustomDebugStringConvertible
public var debugDescription: String {
return self.description
}
// MARK: Internal
internal init(_ error: ErrorType?) {
switch error {
case (let error as CoreStoreError)?:
self = error
case (let error as NSError)?:
self = .InternalError(error)
default:
self = .Unknown
}
}
}
// MARK: - CoreStoreErrorCode
/**
The `NSError` error domain for `CoreStore`.
*/
@available(*, deprecated=2.0.0, message="Use CoreStoreError enum values instead.")
public let CoreStoreErrorDomain = "com.corestore.error"
/**
The `NSError` error codes for `CoreStoreErrorDomain`.
*/
@available(*, deprecated=2.0.0, message="Use CoreStoreError enum values instead.")
public enum CoreStoreErrorCode: Int {
/**
@@ -49,11 +137,6 @@ public enum CoreStoreErrorCode: Int {
*/
case DifferentPersistentStoreExistsAtURL
/**
The `NSPersistentStore` specified could not be found.
*/
case PersistentStoreNotFound
/**
An `NSMappingModel` could not be found for a specific source and destination model versions.
*/
@@ -70,32 +153,8 @@ public enum CoreStoreErrorCode: Int {
public extension NSError {
/**
If the error's domain is equal to `CoreStoreErrorDomain`, returns the associated `CoreStoreErrorCode`. For other domains, returns `nil`.
*/
public var coreStoreErrorCode: CoreStoreErrorCode? {
return (self.domain == CoreStoreErrorDomain
? CoreStoreErrorCode(rawValue: self.code)
: nil)
}
// MARK: Internal
internal convenience init(coreStoreErrorCode: CoreStoreErrorCode) {
self.init(coreStoreErrorCode: coreStoreErrorCode, userInfo: nil)
}
internal convenience init(coreStoreErrorCode: CoreStoreErrorCode, userInfo: [NSObject: AnyObject]?) {
self.init(
domain: CoreStoreErrorDomain,
code: coreStoreErrorCode.rawValue,
userInfo: userInfo)
}
internal var isCoreDataMigrationError: Bool {
let code = self.code
@@ -104,4 +163,18 @@ public extension NSError {
|| code == NSMigrationError)
&& self.domain == NSCocoaErrorDomain
}
// MARK: Deprecated
/**
If the error's domain is equal to `CoreStoreErrorDomain`, returns the associated `CoreStoreErrorCode`. For other domains, returns `nil`.
*/
@available(*, deprecated=2.0.0, message="Use CoreStoreError enum values instead.")
public var coreStoreErrorCode: CoreStoreErrorCode? {
return (self.domain == CoreStoreErrorDomain
? CoreStoreErrorCode(rawValue: self.code)
: nil)
}
}

View File

@@ -96,8 +96,8 @@ internal extension NSManagedObjectContext {
}
catch {
CoreStore.handleError(
error as NSError,
CoreStore.log(
CoreStoreError(error),
"Failed to obtain permanent ID(s) for \(numberOfInsertedObjects) inserted object(s)."
)
}

View File

@@ -46,8 +46,8 @@ internal extension NSManagedObjectContext {
}
catch {
CoreStore.handleError(
error as NSError,
CoreStore.log(
CoreStoreError(error),
"Failed to obtain permanent ID for object."
)
return nil
@@ -61,8 +61,8 @@ internal extension NSManagedObjectContext {
}
catch {
CoreStore.handleError(
error as NSError,
CoreStore.log(
CoreStoreError(error),
"Failed to load existing \(typeName(object)) in context."
)
return nil
@@ -88,7 +88,7 @@ internal extension NSManagedObjectContext {
}
var fetchResults: [T]?
var fetchError: NSError?
var fetchError: ErrorType?
self.performBlockAndWait {
do {
@@ -97,13 +97,13 @@ internal extension NSManagedObjectContext {
}
catch {
fetchError = error as NSError
fetchError = error
}
}
if fetchResults == nil {
CoreStore.handleError(
fetchError ?? NSError(coreStoreErrorCode: .UnknownError),
CoreStore.log(
CoreStoreError(fetchError),
"Failed executing fetch request."
)
return nil
@@ -131,7 +131,7 @@ internal extension NSManagedObjectContext {
}
var fetchResults: [T]?
var fetchError: NSError?
var fetchError: ErrorType?
self.performBlockAndWait {
do {
@@ -140,13 +140,13 @@ internal extension NSManagedObjectContext {
}
catch {
fetchError = error as NSError
fetchError = error
}
}
if fetchResults == nil {
CoreStore.handleError(
fetchError ?? NSError(coreStoreErrorCode: .UnknownError),
CoreStore.log(
CoreStoreError(fetchError),
"Failed executing fetch request."
)
return nil
@@ -178,8 +178,8 @@ internal extension NSManagedObjectContext {
}
if count == NSNotFound {
CoreStore.handleError(
error ?? NSError(coreStoreErrorCode: .UnknownError),
CoreStore.log(
CoreStoreError(error),
"Failed executing fetch request."
)
return nil
@@ -207,7 +207,7 @@ internal extension NSManagedObjectContext {
}
var fetchResults: [NSManagedObjectID]?
var fetchError: NSError?
var fetchError: ErrorType?
self.performBlockAndWait {
do {
@@ -216,13 +216,13 @@ internal extension NSManagedObjectContext {
}
catch {
fetchError = error as NSError
fetchError = error
}
}
if fetchResults == nil {
CoreStore.handleError(
fetchError ?? NSError(coreStoreErrorCode: .UnknownError),
CoreStore.log(
CoreStoreError(fetchError),
"Failed executing fetch request."
)
return nil
@@ -250,7 +250,7 @@ internal extension NSManagedObjectContext {
}
var fetchResults: [NSManagedObjectID]?
var fetchError: NSError?
var fetchError: ErrorType?
self.performBlockAndWait {
do {
@@ -259,13 +259,13 @@ internal extension NSManagedObjectContext {
}
catch {
fetchError = error as NSError
fetchError = error
}
}
if fetchResults == nil {
CoreStore.handleError(
fetchError ?? NSError(coreStoreErrorCode: .UnknownError),
CoreStore.log(
CoreStoreError(fetchError),
"Failed executing fetch request."
)
return nil
@@ -295,7 +295,7 @@ internal extension NSManagedObjectContext {
}
var numberOfDeletedObjects: Int?
var fetchError: NSError?
var fetchError: ErrorType?
self.performBlockAndWait {
autoreleasepool {
@@ -311,14 +311,14 @@ internal extension NSManagedObjectContext {
}
catch {
fetchError = error as NSError
fetchError = error
}
}
}
if numberOfDeletedObjects == nil {
CoreStore.handleError(
fetchError ?? NSError(coreStoreErrorCode: .UnknownError),
CoreStore.log(
CoreStoreError(fetchError),
"Failed executing fetch request."
)
return nil
@@ -347,7 +347,7 @@ internal extension NSManagedObjectContext {
}
var fetchResults: [AnyObject]?
var fetchError: NSError?
var fetchError: ErrorType?
self.performBlockAndWait {
do {
@@ -356,7 +356,7 @@ internal extension NSManagedObjectContext {
}
catch {
fetchError = error as NSError
fetchError = error
}
}
if let fetchResults = fetchResults {
@@ -369,8 +369,8 @@ internal extension NSManagedObjectContext {
return nil
}
CoreStore.handleError(
fetchError ?? NSError(coreStoreErrorCode: .UnknownError),
CoreStore.log(
CoreStoreError(fetchError),
"Failed executing fetch request."
)
return nil
@@ -396,7 +396,7 @@ internal extension NSManagedObjectContext {
}
var fetchResults: [AnyObject]?
var fetchError: NSError?
var fetchError: ErrorType?
self.performBlockAndWait {
do {
@@ -405,7 +405,7 @@ internal extension NSManagedObjectContext {
}
catch {
fetchError = error as NSError
fetchError = error
}
}
if let fetchResults = fetchResults {
@@ -413,8 +413,8 @@ internal extension NSManagedObjectContext {
return Select<NSDictionary>.ReturnType.fromResultObjects(fetchResults)
}
CoreStore.handleError(
fetchError ?? NSError(coreStoreErrorCode: .UnknownError),
CoreStore.log(
CoreStoreError(fetchError),
"Failed executing fetch request."
)
return nil

View File

@@ -93,8 +93,8 @@ internal extension NSManagedObjectContext {
}
catch {
let saveError = error as NSError
CoreStore.handleError(
let saveError = CoreStoreError(error)
CoreStore.log(
saveError,
"Failed to save \(typeName(NSManagedObjectContext))."
)
@@ -141,8 +141,8 @@ internal extension NSManagedObjectContext {
}
catch {
let saveError = error as NSError
CoreStore.handleError(
let saveError = CoreStoreError(error)
CoreStore.log(
saveError,
"Failed to save \(typeName(NSManagedObjectContext))."
)

View File

@@ -84,6 +84,6 @@ internal extension NSPersistentStoreCoordinator {
return store
}
throw storeError ?? NSError(coreStoreErrorCode: .UnknownError)
throw CoreStoreError(storeError)
}
}

View File

@@ -49,9 +49,9 @@ public extension CoreStore {
)
}
internal static func handleError(error: NSError, _ message: String, fileName: StaticString = #file, lineNumber: Int = #line, functionName: StaticString = #function) {
internal static func log(error: CoreStoreError, _ message: String, fileName: StaticString = #file, lineNumber: Int = #line, functionName: StaticString = #function) {
self.logger.handleError(
self.logger.log(
error: error,
message: message,
fileName: fileName,

View File

@@ -67,7 +67,7 @@ public protocol CoreStoreLogger {
:lineNumber: the source line number
:functionName: the source function name
*/
func handleError(error error: NSError, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString)
func log(error error: CoreStoreError, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString)
/**
Handles assertions made throughout the `CoreStore` framework.
@@ -79,6 +79,24 @@ public protocol CoreStoreLogger {
:functionName: the source function name
*/
func assert(@autoclosure condition: () -> Bool, @autoclosure message: () -> String, fileName: StaticString, lineNumber: Int, functionName: StaticString)
// MARK: Deprecated
/**
Deprecated. Use `log(error:message:fileName:lineNumber:functionName:)` instead.
*/
@available(*, deprecated=2.0.0, message="Use log(error:message:fileName:lineNumber:functionName:) instead.")
func handleError(error error: NSError, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString)
}
extension CoreStoreLogger {
/**
Deprecated. Use `log(error:message:fileName:lineNumber:functionName:)` instead.
*/
@available(*, deprecated=2.0.0, message="Use log(error:message:fileName:lineNumber:functionName:) instead.")
public func handleError(error error: NSError, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString) {}
}

View File

@@ -66,7 +66,7 @@ public final class DefaultLogger: CoreStoreLogger {
#endif
}
public func handleError(error error: NSError, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString) {
public func log(error error: CoreStoreError, message: String, fileName: StaticString, lineNumber: Int, functionName: StaticString) {
#if DEBUG
Swift.print("⚠️ [CoreStore: Error] \((fileName.stringValue as NSString).lastPathComponent):\(lineNumber) \(functionName)\n ↪︎ \(message)\n \(error)\n")

View File

@@ -101,8 +101,8 @@ public extension DataStack {
}
catch {
let storeError = error as NSError
CoreStore.handleError(
let storeError = CoreStoreError(error)
CoreStore.log(
storeError,
"Failed to add \(typeName(storage)) to the stack."
)
@@ -187,8 +187,8 @@ public extension DataStack {
return nil
}
let error = NSError(coreStoreErrorCode: .DifferentPersistentStoreExistsAtURL)
CoreStore.handleError(
let error = CoreStoreError.DifferentStorageExistsAtURL(existingPersistentStoreURL: fileURL)
CoreStore.log(
error,
"Failed to add \(typeName(storage)) at \"\(fileURL)\" because a different \(typeName(NSPersistentStore)) at that URL already exists."
)
@@ -214,7 +214,7 @@ public extension DataStack {
metadata: metadata,
completion: { (result) -> Void in
if case .Failure(let error) = result {
if case .Failure(.InternalError(let error)) = result {
if storage.localStorageOptions.contains(.RecreateStoreOnModelMismatch) && error.isCoreDataMigrationError {
@@ -230,7 +230,7 @@ public extension DataStack {
}
catch {
completion(SetupResult(error as NSError))
completion(SetupResult(error))
}
return
}
@@ -247,7 +247,7 @@ public extension DataStack {
}
catch {
completion(SetupResult(error as NSError))
completion(SetupResult(error))
}
}
)
@@ -265,11 +265,12 @@ public extension DataStack {
}
catch {
CoreStore.handleError(
error as NSError,
let storeError = CoreStoreError(error)
CoreStore.log(
storeError,
"Failed to load SQLite \(typeName(NSPersistentStore)) metadata."
)
throw error
throw storeError
}
}
}
@@ -306,11 +307,12 @@ public extension DataStack {
}
catch {
CoreStore.handleError(
error as NSError,
let metadataError = CoreStoreError(error)
CoreStore.log(
metadataError,
"Failed to load \(typeName(storage)) metadata from URL \"\(fileURL)\"."
)
throw error
throw metadataError
}
}
}
@@ -342,8 +344,12 @@ public extension DataStack {
guard let migrationSteps = self.computeMigrationFromStorage(storage, metadata: metadata) else {
let error = NSError(coreStoreErrorCode: .MappingModelNotFound)
CoreStore.handleError(
let error = CoreStoreError.MappingModelNotFound(
storage: storage,
targetModel: self.model,
targetModelVersion: self.modelVersion
)
CoreStore.log(
error,
"Failed to find migration steps from the \(typeName(storage)) at URL \"\(fileURL)\" to version model \"\(self.modelVersion)\"."
)
@@ -352,8 +358,8 @@ public extension DataStack {
if migrationSteps.count > 1 && storage.localStorageOptions.contains(.PreventProgressiveMigration) {
let error = NSError(coreStoreErrorCode: .ProgressiveMigrationRequired)
CoreStore.handleError(
let error = CoreStoreError.ProgressiveMigrationRequired(storage: storage)
CoreStore.log(
error,
"Failed to find migration mapping from the \(typeName(storage)) at URL \"\(fileURL)\" to version model \"\(self.modelVersion)\" without requiring progessive migrations."
)
@@ -369,11 +375,12 @@ public extension DataStack {
}
catch {
CoreStore.handleError(
error as NSError,
let metadataError = CoreStoreError(error)
CoreStore.log(
metadataError,
"Failed to load \(typeName(storage)) metadata from URL \"\(fileURL)\"."
)
throw error
throw metadataError
}
}
}
@@ -385,14 +392,19 @@ public extension DataStack {
guard let migrationSteps = self.computeMigrationFromStorage(storage, metadata: metadata) else {
CoreStore.handleError(
NSError(coreStoreErrorCode: .MappingModelNotFound),
"Failed to find migration steps from \(typeName(storage)) at URL \"\(storage.fileURL )\" to version model \"\(model)\"."
let error = CoreStoreError.MappingModelNotFound(
storage: storage,
targetModel: self.model,
targetModelVersion: self.modelVersion
)
CoreStore.log(
error,
"Failed to find migration steps from \(typeName(storage)) at URL \"\(storage.fileURL)\" to version model \"\(self.model)\"."
)
GCDQueue.Main.async {
completion(MigrationResult(.MappingModelNotFound))
completion(MigrationResult(error))
}
return nil
}
@@ -409,14 +421,14 @@ public extension DataStack {
}
else if numberOfMigrations > 1 && storage.localStorageOptions.contains(.PreventProgressiveMigration) {
let error = NSError(coreStoreErrorCode: .ProgressiveMigrationRequired)
CoreStore.handleError(
let error = CoreStoreError.ProgressiveMigrationRequired(storage: storage)
CoreStore.log(
error,
"Failed to find migration mapping from the \(typeName(storage)) at URL \"\(storage.fileURL)\" to version model \"\(self.modelVersion)\" without requiring progessive migrations."
)
GCDQueue.Main.async {
completion(MigrationResult(.ProgressiveMigrationRequired))
completion(MigrationResult(error))
}
return nil
}
@@ -458,7 +470,7 @@ public extension DataStack {
}
catch {
migrationResult = MigrationResult(error as NSError)
migrationResult = MigrationResult(error)
cancelled = true
}
}
@@ -619,12 +631,13 @@ public extension DataStack {
let sourceVersion = migrationManager.sourceModel.currentModelVersion ?? "???"
let destinationVersion = migrationManager.destinationModel.currentModelVersion ?? "???"
CoreStore.handleError(
error as NSError,
let migrationError = CoreStoreError(error)
CoreStore.log(
migrationError,
"Failed to migrate from version model \"\(sourceVersion)\" to version model \"\(destinationVersion)\"."
)
throw error
throw migrationError
}
do {
@@ -649,12 +662,13 @@ public extension DataStack {
let sourceVersion = migrationManager.sourceModel.currentModelVersion ?? "???"
let destinationVersion = migrationManager.destinationModel.currentModelVersion ?? "???"
CoreStore.handleError(
error as NSError,
let fileError = CoreStoreError(error)
CoreStore.log(
fileError,
"Failed to save store after migrating from version model \"\(sourceVersion)\" to version model \"\(destinationVersion)\"."
)
throw error
throw fileError
}
}
@@ -679,7 +693,7 @@ public extension DataStack {
completion(PersistentStoreResult(self.persistentStoreForStorage(storage)!))
case .Failure(let error):
completion(PersistentStoreResult(error))
completion(PersistentStoreResult(error as NSError))
}
}
)
@@ -713,7 +727,7 @@ public extension DataStack {
completion(PersistentStoreResult(self.persistentStoreForStorage(storage)!))
case .Failure(let error):
completion(PersistentStoreResult(error))
completion(PersistentStoreResult(error as NSError))
}
}
)
@@ -742,7 +756,7 @@ public extension DataStack {
completion(PersistentStoreResult(self.persistentStoreForStorage(storage)!))
case .Failure(let error):
completion(PersistentStoreResult(error))
completion(PersistentStoreResult(error as NSError))
}
}
)

View File

@@ -52,7 +52,7 @@ import Foundation
case .Success(let hasChanges):
// hasChanges indicates if there were changes or not
case .Failure(let error):
// error is the NSError instance for the failure
// error is a CoreStoreError enum value
}
}
```
@@ -65,9 +65,9 @@ public enum MigrationResult {
case Success([MigrationType])
/**
`SaveResult.Failure` indicates that the migration failed. The associated object for this value is the related `NSError` instance.
`SaveResult.Failure` indicates that the migration failed. The associated object for this value is the a `CoreStoreError` enum value.
*/
case Failure(NSError)
case Failure(CoreStoreError)
// MARK: Internal
@@ -77,19 +77,14 @@ public enum MigrationResult {
self = .Success(migrationTypes)
}
internal init(_ error: NSError) {
internal init(_ error: CoreStoreError) {
self = .Failure(error)
}
internal init(_ errorCode: CoreStoreErrorCode) {
internal init(_ error: ErrorType) {
self.init(errorCode, userInfo: nil)
}
internal init(_ errorCode: CoreStoreErrorCode, userInfo: [NSObject: AnyObject]?) {
self.init(NSError(coreStoreErrorCode: errorCode, userInfo: userInfo))
self = .Failure(CoreStoreError(error))
}
}

View File

@@ -52,7 +52,7 @@ import Foundation
case .Success(let hasChanges):
// hasChanges indicates if there were changes or not
case .Failure(let error):
// error is the NSError instance for the failure
// error is a CoreStoreError enum value
}
}
```
@@ -65,9 +65,9 @@ public enum SaveResult {
case Success(hasChanges: Bool)
/**
`SaveResult.Failure` indicates that the `commit()` for the transaction failed. The associated object for this value is the related `NSError` instance.
`SaveResult.Failure` indicates that the `commit()` for the transaction failed. The associated object for this value is a `CoreStoreError` enum value.
*/
case Failure(NSError)
case Failure(CoreStoreError)
// MARK: Internal
@@ -77,20 +77,10 @@ public enum SaveResult {
self = .Success(hasChanges: hasChanges)
}
internal init(_ error: NSError) {
internal init(_ error: CoreStoreError) {
self = .Failure(error)
}
internal init(_ errorCode: CoreStoreErrorCode) {
self.init(errorCode, userInfo: nil)
}
internal init(_ errorCode: CoreStoreErrorCode, userInfo: [NSObject: AnyObject]?) {
self.init(NSError(coreStoreErrorCode: errorCode, userInfo: userInfo))
}
}

View File

@@ -132,6 +132,9 @@ public extension CoreStore {
/**
Deprecated. Use `addStorageAndWait(_:)` by passing a `InMemoryStore` instance.
```
try CoreStore.addStorage(InMemoryStore(configuration: configuration))
```
*/
@available(*, deprecated=2.0.0, obsoleted=2.0.0, message="Use addStorageAndWait(_:) by passing an InMemoryStore instance.")
public static func addInMemoryStoreAndWait(configuration configuration: String? = nil) throws -> NSPersistentStore {
@@ -141,6 +144,15 @@ public extension CoreStore {
/**
Deprecated. Use `addStorageAndWait(_:)` by passing a `LegacySQLiteStore` instance.
```
try CoreStore.addStorage(
LegacySQLiteStore(
fileName: fileName,
configuration: configuration,
localStorageOptions: .RecreateStoreOnModelMismatch
)
)
```
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
*/
@available(*, deprecated=2.0.0, message="Use addStorageAndWait(_:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
@@ -155,6 +167,15 @@ public extension CoreStore {
/**
Deprecated. Use `addStorageAndWait(_:)` by passing a `LegacySQLiteStore` instance.
```
try CoreStore.addStorage(
LegacySQLiteStore(
fileURL: fileURL,
configuration: configuration,
localStorageOptions: .RecreateStoreOnModelMismatch
)
)
```
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
*/
@available(*, deprecated=2.0.0, message="Use addStorageAndWait(_:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")

View File

@@ -168,11 +168,12 @@ public final class DataStack {
}
catch {
CoreStore.handleError(
error as NSError,
let storeError = CoreStoreError(error)
CoreStore.log(
storeError,
"Failed to add \(typeName(storage)) to the stack."
)
throw error
throw storeError
}
}
@@ -222,8 +223,8 @@ public final class DataStack {
return existingStorage
}
let error = NSError(coreStoreErrorCode: .DifferentPersistentStoreExistsAtURL)
CoreStore.handleError(
let error = CoreStoreError.DifferentStorageExistsAtURL(existingPersistentStoreURL: fileURL)
CoreStore.log(
error,
"Failed to add \(typeName(storage)) at \"\(fileURL)\" because a different \(typeName(NSPersistentStore)) at that URL already exists."
)
@@ -271,11 +272,12 @@ public final class DataStack {
}
catch {
CoreStore.handleError(
error as NSError,
let storeError = CoreStoreError(error)
CoreStore.log(
storeError,
"Failed to add \(typeName(storage)) to the stack."
)
throw error
throw storeError
}
}
}
@@ -410,6 +412,9 @@ public final class DataStack {
/**
Deprecated. Use `addStorageAndWait(_:)` by passing a `InMemoryStore` instance.
```
try dataStack.addStorage(InMemoryStore(configuration: configuration))
```
*/
@available(*, deprecated=2.0.0, message="Use addStorageAndWait(_:) by passing an InMemoryStore instance.")
public func addInMemoryStoreAndWait(configuration configuration: String? = nil) throws -> NSPersistentStore {
@@ -420,7 +425,15 @@ public final class DataStack {
/**
Deprecated. Use `addStorageAndWait(_:)` by passing a `LegacySQLiteStore` instance.
```
try dataStack.addStorage(
LegacySQLiteStore(
fileName: fileName,
configuration: configuration,
localStorageOptions: .RecreateStoreOnModelMismatch
)
)
```
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
*/
@available(*, deprecated=2.0.0, message="Use addStorageAndWait(_:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")
@@ -438,7 +451,15 @@ public final class DataStack {
/**
Deprecated. Use `addStorageAndWait(_:)` by passing a `LegacySQLiteStore` instance.
```
try dataStack.addStorage(
LegacySQLiteStore(
fileURL: fileURL,
configuration: configuration,
localStorageOptions: .RecreateStoreOnModelMismatch
)
)
```
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was using this method prior to 2.0.0, make sure to use `LegacySQLiteStore`.
*/
@available(*, deprecated=2.0.0, message="Use addStorageAndWait(_:) by passing a LegacySQLiteStore instance. Warning: The default SQLite file location for the LegacySQLiteStore and SQLiteStore are different. If the app was using this method prior to 2.0.0, make sure to use LegacySQLiteStore.")

View File

@@ -1,5 +1,5 @@
//
// PersistentStoreResult.swift
// SetupResult.swift
// CoreStore
//
// Copyright © 2014 John Rommel Estropia
@@ -54,7 +54,7 @@ import CoreData
case .Success(let storage):
// storage is the related StorageInterface instance
case .Failure(let error):
// error is the NSError instance for the failure
// error is the CoreStoreError enum value for the failure
}
}
)
@@ -68,9 +68,9 @@ public enum SetupResult<T: StorageInterface>: BooleanType {
case Success(T)
/**
`SetupResult.Failure` indicates that the storage setup failed. The associated object for this value is the related `NSError` instance.
`SetupResult.Failure` indicates that the storage setup failed. The associated object for this value is the related `CoreStoreError` enum value.
*/
case Failure(NSError)
case Failure(CoreStoreError)
// MARK: BooleanType
@@ -92,19 +92,14 @@ public enum SetupResult<T: StorageInterface>: BooleanType {
self = .Success(storage)
}
internal init(_ error: NSError) {
internal init(_ error: CoreStoreError) {
self = .Failure(error)
}
internal init(_ errorCode: CoreStoreErrorCode) {
internal init(_ error: ErrorType) {
self.init(errorCode, userInfo: nil)
}
internal init(_ errorCode: CoreStoreErrorCode, userInfo: [NSObject: AnyObject]?) {
self.init(NSError(coreStoreErrorCode: errorCode, userInfo: userInfo))
self = .Failure(CoreStoreError(error))
}
}
@@ -152,14 +147,4 @@ public enum PersistentStoreResult: BooleanType {
self = .Failure(error)
}
internal init(_ errorCode: CoreStoreErrorCode) {
self.init(errorCode, userInfo: nil)
}
internal init(_ errorCode: CoreStoreErrorCode, userInfo: [NSObject: AnyObject]?) {
self.init(NSError(coreStoreErrorCode: errorCode, userInfo: userInfo))
}
}