updated error handling for DataStack methods, updated migration demo app

This commit is contained in:
John Rommel Estropia
2015-07-12 19:20:38 +09:00
parent 8cfe8e2500
commit a64bcc474e
28 changed files with 773 additions and 393 deletions

View File

@@ -67,7 +67,6 @@ public final class DataStack {
self.coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
self.rootSavingContext = NSManagedObjectContext.rootSavingContextForCoordinator(self.coordinator)
self.mainContext = NSManagedObjectContext.mainContextForRootContext(self.rootSavingContext)
self.bundle = bundle
self.model = model
self.migrationChain = migrationChain
@@ -75,7 +74,7 @@ public final class DataStack {
}
/**
Returns the `DataStack`'s model version. The version string is the same as the name of the .xcdatamodeld file.
Returns the `DataStack`'s model version. The version string is the same as the name of the version-specific .xcdatamodeld file.
*/
public var modelVersion: String {
@@ -85,10 +84,10 @@ public final class DataStack {
/**
Adds an in-memory store to the stack.
- parameter configuration: an optional configuration name from the model file. If not specified, defaults to nil.
- returns: a `PersistentStoreResult` indicating success or failure.
- parameter configuration: an optional configuration name from the model file. If not specified, defaults to `nil`.
- returns: the `NSPersistentStore` added to the stack.
*/
public func addInMemoryStore(configuration configuration: String? = nil) -> PersistentStoreResult {
public func addInMemoryStore(configuration configuration: String? = nil) throws -> NSPersistentStore {
let coordinator = self.coordinator;
@@ -114,25 +113,15 @@ public final class DataStack {
if let store = store {
self.updateMetadataForPersistentStore(store)
return PersistentStoreResult(store)
return store
}
if let error = storeError {
CoreStore.handleError(
error,
"Failed to add in-memory \(typeName(NSPersistentStore))."
)
return PersistentStoreResult(error)
}
else {
CoreStore.handleError(
NSError(coreStoreErrorCode: .UnknownError),
"Failed to add in-memory \(typeName(NSPersistentStore))."
)
return PersistentStoreResult(.UnknownError)
}
let error = storeError ?? NSError(coreStoreErrorCode: .UnknownError)
CoreStore.handleError(
error,
"Failed to add in-memory \(typeName(NSPersistentStore)) to the stack."
)
throw error
}
/**
@@ -142,11 +131,11 @@ public final class DataStack {
- parameter configuration: an optional configuration name from the model file. If not specified, defaults to `nil`, the "Default" configuration. Note that if you have multiple configurations, you will need to specify a different `fileName` explicitly for each of them.
- parameter automigrating: Set to true to configure Core Data auto-migration, or false to disable. If not specified, defaults to true.
- parameter resetStoreOnMigrationFailure: Set to true to delete the store on migration failure; or set to false to throw exceptions on failure instead. Typically should only be set to true when debugging, or if the persistent store can be recreated easily. If not specified, defaults to false
- returns: a `PersistentStoreResult` indicating success or failure.
- returns: the `NSPersistentStore` added to the stack.
*/
public func addSQLiteStoreAndWait(fileName fileName: String, configuration: String? = nil, automigrating: Bool = true, resetStoreOnMigrationFailure: Bool = false) -> PersistentStoreResult {
public func addSQLiteStoreAndWait(fileName fileName: String, configuration: String? = nil, automigrating: Bool = true, resetStoreOnMigrationFailure: Bool = false) throws -> NSPersistentStore {
return self.addSQLiteStoreAndWait(
return try self.addSQLiteStoreAndWait(
fileURL: applicationSupportDirectory.URLByAppendingPathComponent(
fileName,
isDirectory: false
@@ -164,9 +153,9 @@ public final class DataStack {
- parameter configuration: an optional configuration name from the model file. If not specified, defaults to `nil`, the "Default" configuration. Note that if you have multiple configurations, you will need to specify a different `fileURL` explicitly for each of them.
- parameter automigrating: Set to true to configure Core Data auto-migration, or false to disable. If not specified, defaults to true.
- parameter resetStoreOnMigrationFailure: Set to true to delete the store on migration failure; or set to false to throw exceptions on failure instead. Typically should only be set to true when debugging, or if the persistent store can be recreated easily. If not specified, defaults to false.
- returns: a `PersistentStoreResult` indicating success or failure.
- returns: the `NSPersistentStore` added to the stack.
*/
public func addSQLiteStoreAndWait(fileURL fileURL: NSURL = defaultSQLiteStoreURL, configuration: String? = nil, automigrating: Bool = true, resetStoreOnMigrationFailure: Bool = false) -> PersistentStoreResult {
public func addSQLiteStoreAndWait(fileURL fileURL: NSURL = defaultSQLiteStoreURL, configuration: String? = nil, automigrating: Bool = true, resetStoreOnMigrationFailure: Bool = false) throws -> NSPersistentStore {
CoreStore.assert(
fileURL.fileURL,
@@ -182,15 +171,16 @@ public final class DataStack {
&& isExistingStoreAutomigrating == automigrating
&& store.configurationName == (configuration ?? Into.defaultConfigurationName) {
return PersistentStoreResult(store)
return store
}
let error = NSError(coreStoreErrorCode: .DifferentPersistentStoreExistsAtURL)
CoreStore.handleError(
NSError(coreStoreErrorCode: .DifferentPersistentStoreExistsAtURL),
error,
"Failed to add SQLite \(typeName(NSPersistentStore)) at \"\(fileURL)\" because a different \(typeName(NSPersistentStore)) at that URL already exists."
)
return PersistentStoreResult(.DifferentPersistentStoreExistsAtURL)
throw error
}
let fileManager = NSFileManager.defaultManager()
@@ -230,7 +220,7 @@ public final class DataStack {
if let store = store {
self.updateMetadataForPersistentStore(store)
return PersistentStoreResult(store)
return store
}
if let error = storeError
@@ -285,16 +275,16 @@ public final class DataStack {
if let store = store {
self.updateMetadataForPersistentStore(store)
return PersistentStoreResult(store)
return store
}
}
let error = storeError ?? NSError(coreStoreErrorCode: .UnknownError)
CoreStore.handleError(
storeError ?? NSError(coreStoreErrorCode: .UnknownError),
error,
"Failed to add SQLite \(typeName(NSPersistentStore)) at \"\(fileURL)\"."
)
return PersistentStoreResult(.UnknownError)
throw error
}
@@ -303,7 +293,6 @@ public final class DataStack {
internal let coordinator: NSPersistentStoreCoordinator
internal let rootSavingContext: NSManagedObjectContext
internal let mainContext: NSManagedObjectContext
internal let bundle: NSBundle
internal let model: NSManagedObjectModel
internal let migrationChain: MigrationChain
internal let childTransactionQueue: GCDQueue = .createSerial("com.corestore.datastack.childtransactionqueue")