WIP: tidy up

This commit is contained in:
John Estropia
2016-03-09 18:49:00 +09:00
parent 67f1bd9a63
commit c85ef16ad0
6 changed files with 54 additions and 47 deletions

View File

@@ -206,6 +206,9 @@
B56321B61BD6521C006C9394 /* WeakObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E84F2D1AFF849C0064E85B /* WeakObject.swift */; };
B56964D41B22FFAD0075EE4A /* DataStack+Migration.swift in Sources */ = {isa = PBXBuildFile; fileRef = B56964D31B22FFAD0075EE4A /* DataStack+Migration.swift */; };
B56965241B356B820075EE4A /* MigrationResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = B56965231B356B820075EE4A /* MigrationResult.swift */; };
B59851491C90289D00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */; };
B598514A1C90289E00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */; };
B598514B1C90289F00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */; };
B59AFF411C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */; };
B59D5C221B5BA34B00453479 /* NSFileManager+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59D5C211B5BA34B00453479 /* NSFileManager+Setup.swift */; };
B5A261211B64BFDB006EB6D3 /* MigrationType.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5A261201B64BFDB006EB6D3 /* MigrationType.swift */; };
@@ -600,7 +603,7 @@
B5E84EDB1AFF84500064E85B /* DataStack.swift */,
B5E84EDE1AFF84500064E85B /* SetupResult.swift */,
B504D0D51B02362500B2BBB1 /* CoreStore+Setup.swift */,
B5FE4DA01C84818B00FA6A91 /* PersistentStores */,
B5FE4DA01C84818B00FA6A91 /* StorageInterfaces */,
);
path = "Setting Up";
sourceTree = "<group>";
@@ -710,7 +713,7 @@
path = Internal;
sourceTree = "<group>";
};
B5FE4DA01C84818B00FA6A91 /* PersistentStores */ = {
B5FE4DA01C84818B00FA6A91 /* StorageInterfaces */ = {
isa = PBXGroup;
children = (
B5FE4DA11C8481E100FA6A91 /* StorageInterface.swift */,
@@ -718,7 +721,7 @@
B5FE4DAB1C85D44E00FA6A91 /* SQLiteStore.swift */,
B5D3F6441C887C0A00C7492A /* LegacySQLiteStore.swift */,
);
path = PersistentStores;
path = StorageInterfaces;
sourceTree = "<group>";
};
/* End PBXGroup section */
@@ -1115,6 +1118,7 @@
82BA18D01C4BBD7100A0916E /* MigrationManager.swift in Sources */,
82BA18C61C4BBD5900A0916E /* DataStack+Migration.swift in Sources */,
82BA18CD1C4BBD7100A0916E /* AssociatedObjects.swift in Sources */,
B59851491C90289D00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */,
82BA18B71C4BBD3F00A0916E /* CoreStore+Querying.swift in Sources */,
82BA18A41C4BBD2200A0916E /* SetupResult.swift in Sources */,
82BA18AA1C4BBD3100A0916E /* BaseDataTransaction.swift in Sources */,
@@ -1213,6 +1217,7 @@
B52DD19B1BE1F92800949AFE /* CoreStoreLogger.swift in Sources */,
B52DD1991BE1F92800949AFE /* DefaultLogger.swift in Sources */,
B52DD1B91BE1F94000949AFE /* CoreStore+Migration.swift in Sources */,
B598514B1C90289F00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */,
B52DD1AA1BE1F93500949AFE /* ClauseTypes.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -1266,6 +1271,7 @@
B56321AE1BD6521C006C9394 /* NotificationObserver.swift in Sources */,
B56321931BD65216006C9394 /* DataStack+Querying.swift in Sources */,
B56321A71BD65216006C9394 /* MigrationResult.swift in Sources */,
B598514A1C90289E00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */,
B56321A11BD65216006C9394 /* ListMonitor.swift in Sources */,
B56321881BD65216006C9394 /* BaseDataTransaction.swift in Sources */,
B56321A31BD65216006C9394 /* DataStack+Migration.swift in Sources */,

View File

@@ -38,24 +38,34 @@ import CoreData
public final class DataStack {
/**
Initializes a `DataStack` from an `NSManagedObjectModel`.
Initializes a `DataStack` from the model with the specified `modelName` in the specified `bundle`.
- parameter modelName: the name of the (.xcdatamodeld) model file. If not specified, the application name (CFBundleName) will be used if it exists, or "CoreData" if it the bundle name was not set.
- parameter bundle: an optional bundle to load models from. If not specified, the main bundle will be used.
- parameter migrationChain: the `MigrationChain` that indicates the sequence of model versions to be used as the order for progressive migrations. If not specified, will default to a non-migrating data stack.
*/
public required init(modelName: String = DataStack.applicationName, bundle: NSBundle = NSBundle.mainBundle(), migrationChain: MigrationChain = nil) {
CoreStore.assert(
migrationChain.valid,
"Invalid migration chain passed to the \(typeName(DataStack)). Check that the model versions' order is correct and that no repetitions or ambiguities exist."
)
public convenience init(modelName: String = DataStack.applicationName, bundle: NSBundle = NSBundle.mainBundle(), migrationChain: MigrationChain = nil) {
let model = NSManagedObjectModel.fromBundle(
bundle,
modelName: modelName,
modelVersionHints: migrationChain.leafVersions
)
self.init(model: model, migrationChain: migrationChain)
}
/**
Initializes a `DataStack` from an `NSManagedObjectModel`.
- parameter model: the `NSManagedObjectModel` for the stack
- parameter migrationChain: the `MigrationChain` that indicates the sequence of model versions to be used as the order for progressive migrations. If not specified, will default to a non-migrating data stack.
*/
public required init(model: NSManagedObjectModel, migrationChain: MigrationChain = nil) {
CoreStore.assert(
migrationChain.valid,
"Invalid migration chain passed to the \(typeName(DataStack)). Check that the model versions' order is correct and that no repetitions or ambiguities exist."
)
self.coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
self.rootSavingContext = NSManagedObjectContext.rootSavingContextForCoordinator(self.coordinator)
@@ -103,6 +113,9 @@ public final class DataStack {
/**
Creates a `StorageInterface` of the specified store type with default values and adds it to the stack. This method blocks until completion.
```
try CoreStore.addStorageAndWait(InMemoryStore)
```
- parameter storeType: the `StorageInterface` type
- returns: the `StorageInterface` added to the stack
@@ -114,6 +127,9 @@ public final class DataStack {
/**
Adds a `StorageInterface` to the stack and blocks until completion.
```
try CoreStore.addStorageAndWait(InMemoryStore(configuration: "Config1"))
```
- parameter storage: the `StorageInterface`
- returns: the `StorageInterface` added to the stack
@@ -125,27 +141,6 @@ public final class DataStack {
"The specified \"\(typeName(storage))\" was already added to the data stack: \(storage)"
)
// TODO: check
// if let store = coordinator.persistentStoreForURL(fileURL) {
//
// guard store.type == storage.dynamicType.storeType
// && store.configurationName == (configuration ?? Into.defaultConfigurationName) else {
//
// let error = NSError(coreStoreErrorCode: .DifferentPersistentStoreExistsAtURL)
// CoreStore.handleError(
// error,
// "Failed to add SQLite \(typeName(NSPersistentStore)) at \"\(fileURL)\" because a different \(typeName(NSPersistentStore)) at that URL already exists."
// )
// throw error
// }
//
// GCDQueue.Main.async {
//
// completion(PersistentStoreResult(store))
// }
// return nil
// }
do {
let persistentStore = try self.coordinator.addPersistentStoreSynchronously(
@@ -169,6 +164,9 @@ public final class DataStack {
/**
Creates a `LocalStorageface` of the specified store type with default values and adds it to the stack. This method blocks until completion.
```
try CoreStore.addStorageAndWait(SQLiteStore)
```
- parameter storeType: the `LocalStorageface` type
- returns: the local storage added to the stack
@@ -180,6 +178,9 @@ public final class DataStack {
/**
Adds a `LocalStorage` to the stack and blocks until completion.
```
try CoreStore.addStorageAndWait(SQLiteStore(configuration: "Config1"))
```
- parameter storage: the local storage
- returns: the local storage added to the stack
@@ -205,7 +206,7 @@ public final class DataStack {
let error = NSError(coreStoreErrorCode: .DifferentPersistentStoreExistsAtURL)
CoreStore.handleError(
error,
"Failed to add SQLite \(typeName(NSPersistentStore)) at \"\(fileURL)\" because a different \(typeName(NSPersistentStore)) at that URL already exists."
"Failed to add \"\(typeName(storage))\" at \"\(fileURL)\" because a different \(typeName(NSPersistentStore)) at that URL already exists."
)
throw error
}
@@ -217,7 +218,19 @@ public final class DataStack {
do {
let coordinator = self.coordinator
let persistentStore = try coordinator.performBlockAndWait { () throws -> NSPersistentStore in
return try coordinator.performBlockAndWait {
let addStorage = { () throws -> T in
let persistentStore = try coordinator.addPersistentStoreWithType(
storage.dynamicType.storeType,
configuration: storage.configuration,
URL: fileURL,
options: storage.storeOptions
)
self.updateMetadataForStorage(storage, persistentStore: persistentStore)
return storage
}
let fileManager = NSFileManager.defaultManager()
do {
@@ -227,12 +240,7 @@ public final class DataStack {
withIntermediateDirectories: true,
attributes: nil
)
return try coordinator.addPersistentStoreWithType(
storage.dynamicType.storeType,
configuration: storage.configuration,
URL: fileURL,
options: storage.storeOptions
)
return try addStorage()
}
catch let error as NSError where storage.resetStoreOnModelMismatch && error.isCoreDataMigrationError {
@@ -243,16 +251,9 @@ public final class DataStack {
)
try _ = self.model[metadata].flatMap(storage.eraseStorageAndWait)
return try coordinator.addPersistentStoreWithType(
storage.dynamicType.storeType,
configuration: storage.configuration,
URL: fileURL,
options: storage.storeOptions
)
return try addStorage()
}
}
self.updateMetadataForStorage(storage, persistentStore: persistentStore)
return storage
}
catch {