mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-29 13:51:48 +02:00
WIP: tidy up
This commit is contained in:
@@ -600,9 +600,9 @@
|
|||||||
B5E84EDA1AFF84500064E85B /* Setting Up */ = {
|
B5E84EDA1AFF84500064E85B /* Setting Up */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
|
B504D0D51B02362500B2BBB1 /* CoreStore+Setup.swift */,
|
||||||
B5E84EDB1AFF84500064E85B /* DataStack.swift */,
|
B5E84EDB1AFF84500064E85B /* DataStack.swift */,
|
||||||
B5E84EDE1AFF84500064E85B /* SetupResult.swift */,
|
B5E84EDE1AFF84500064E85B /* SetupResult.swift */,
|
||||||
B504D0D51B02362500B2BBB1 /* CoreStore+Setup.swift */,
|
|
||||||
B5FE4DA01C84818B00FA6A91 /* StorageInterfaces */,
|
B5FE4DA01C84818B00FA6A91 /* StorageInterfaces */,
|
||||||
);
|
);
|
||||||
path = "Setting Up";
|
path = "Setting Up";
|
||||||
|
|||||||
@@ -391,12 +391,7 @@ class CoreStoreTests: XCTestCase {
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
|
|
||||||
let defaultDirectory = NSFileManager.defaultManager().URLsForDirectory(
|
try NSFileManager.defaultManager().removeItemAtURL(SQLiteStore.defaultRootDirectory)
|
||||||
.ApplicationSupportDirectory,
|
|
||||||
inDomains: .UserDomainMask
|
|
||||||
).first!
|
|
||||||
let fileManager = NSFileManager.defaultManager()
|
|
||||||
try fileManager.removeItemAtURL(defaultDirectory)
|
|
||||||
}
|
}
|
||||||
catch _ { }
|
catch _ { }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,8 +58,24 @@ public extension CoreStore {
|
|||||||
return self.defaultStack.entityDescriptionForType(type)
|
return self.defaultStack.entityDescriptionForType(type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Creates an `SQLiteStore` with default parameters and adds it to the `defaultStack`. This method blocks until completion.
|
||||||
|
```
|
||||||
|
try CoreStore.addStorageAndWait()
|
||||||
|
```
|
||||||
|
|
||||||
|
- returns: the local SQLite storage added to the `defaultStack`
|
||||||
|
*/
|
||||||
|
public static func addStorageAndWait() throws -> SQLiteStore {
|
||||||
|
|
||||||
|
return try self.defaultStack.addStorageAndWait(SQLiteStore)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Creates a `StorageInterface` of the specified store type with default values and adds it to the `defaultStack`. This method blocks until completion.
|
Creates a `StorageInterface` of the specified store type with default values and adds it to the `defaultStack`. This method blocks until completion.
|
||||||
|
```
|
||||||
|
try CoreStore.addStorageAndWait(InMemoryStore)
|
||||||
|
```
|
||||||
|
|
||||||
- parameter storeType: the `StorageInterface` type
|
- parameter storeType: the `StorageInterface` type
|
||||||
- returns: the `StorageInterface` added to the `defaultStack`
|
- returns: the `StorageInterface` added to the `defaultStack`
|
||||||
@@ -71,6 +87,9 @@ public extension CoreStore {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
Adds a `StorageInterface` to the `defaultStack` and blocks until completion.
|
Adds a `StorageInterface` to the `defaultStack` and blocks until completion.
|
||||||
|
```
|
||||||
|
try CoreStore.addStorageAndWait(InMemoryStore(configuration: "Config1"))
|
||||||
|
```
|
||||||
|
|
||||||
- parameter storage: the `StorageInterface`
|
- parameter storage: the `StorageInterface`
|
||||||
- returns: the `StorageInterface` added to the `defaultStack`
|
- returns: the `StorageInterface` added to the `defaultStack`
|
||||||
@@ -82,9 +101,12 @@ public extension CoreStore {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
Creates a `LocalStorageface` of the specified store type with default values and adds it to the `defaultStack`. This method blocks until completion.
|
Creates a `LocalStorageface` of the specified store type with default values and adds it to the `defaultStack`. This method blocks until completion.
|
||||||
|
```
|
||||||
|
try CoreStore.addStorageAndWait(SQLiteStore)
|
||||||
|
```
|
||||||
|
|
||||||
- parameter storeType: the `LocalStorageface` type
|
- parameter storeType: the `LocalStorageface` type
|
||||||
- returns: the local storage added to the stack
|
- returns: the local storage added to the `defaultStack`
|
||||||
*/
|
*/
|
||||||
public static func addStorageAndWait<T: LocalStorage where T: DefaultInitializableStore>(storageType: T.Type) throws -> T {
|
public static func addStorageAndWait<T: LocalStorage where T: DefaultInitializableStore>(storageType: T.Type) throws -> T {
|
||||||
|
|
||||||
@@ -92,10 +114,13 @@ public extension CoreStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Adds a `LocalStorage` to the stack and blocks until completion.
|
Adds a `LocalStorage` to the `defaultStack` and blocks until completion.
|
||||||
|
```
|
||||||
|
try CoreStore.addStorageAndWait(SQLiteStore(configuration: "Config1"))
|
||||||
|
```
|
||||||
|
|
||||||
- parameter storage: the local storage
|
- parameter storage: the local storage
|
||||||
- returns: the local storage added to the stack
|
- returns: the local storage added to the `defaultStack`
|
||||||
*/
|
*/
|
||||||
public static func addStorageAndWait<T: LocalStorage>(storage: T) throws -> T {
|
public static func addStorageAndWait<T: LocalStorage>(storage: T) throws -> T {
|
||||||
|
|
||||||
|
|||||||
@@ -111,10 +111,23 @@ public final class DataStack {
|
|||||||
return self.coordinator.managedObjectIDForURIRepresentation(url)
|
return self.coordinator.managedObjectIDForURIRepresentation(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Creates an `SQLiteStore` with default parameters and adds it to the stack. This method blocks until completion.
|
||||||
|
```
|
||||||
|
try dataStack.addStorageAndWait()
|
||||||
|
```
|
||||||
|
|
||||||
|
- returns: the local SQLite storage added to the stack
|
||||||
|
*/
|
||||||
|
public func addStorageAndWait() throws -> SQLiteStore {
|
||||||
|
|
||||||
|
return try self.addStorageAndWait(SQLiteStore)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Creates a `StorageInterface` of the specified store type with default values and adds it to the stack. This method blocks until completion.
|
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)
|
try dataStack.addStorageAndWait(InMemoryStore)
|
||||||
```
|
```
|
||||||
|
|
||||||
- parameter storeType: the `StorageInterface` type
|
- parameter storeType: the `StorageInterface` type
|
||||||
@@ -128,7 +141,7 @@ public final class DataStack {
|
|||||||
/**
|
/**
|
||||||
Adds a `StorageInterface` to the stack and blocks until completion.
|
Adds a `StorageInterface` to the stack and blocks until completion.
|
||||||
```
|
```
|
||||||
try CoreStore.addStorageAndWait(InMemoryStore(configuration: "Config1"))
|
try dataStack.addStorageAndWait(InMemoryStore(configuration: "Config1"))
|
||||||
```
|
```
|
||||||
|
|
||||||
- parameter storage: the `StorageInterface`
|
- parameter storage: the `StorageInterface`
|
||||||
@@ -165,7 +178,7 @@ 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.
|
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)
|
try dataStack.addStorageAndWait(SQLiteStore)
|
||||||
```
|
```
|
||||||
|
|
||||||
- parameter storeType: the `LocalStorageface` type
|
- parameter storeType: the `LocalStorageface` type
|
||||||
@@ -179,7 +192,7 @@ public final class DataStack {
|
|||||||
/**
|
/**
|
||||||
Adds a `LocalStorage` to the stack and blocks until completion.
|
Adds a `LocalStorage` to the stack and blocks until completion.
|
||||||
```
|
```
|
||||||
try CoreStore.addStorageAndWait(SQLiteStore(configuration: "Config1"))
|
try dataStack.addStorageAndWait(SQLiteStore(configuration: "Config1"))
|
||||||
```
|
```
|
||||||
|
|
||||||
- parameter storage: the local storage
|
- parameter storage: the local storage
|
||||||
@@ -206,7 +219,7 @@ public final class DataStack {
|
|||||||
let error = NSError(coreStoreErrorCode: .DifferentPersistentStoreExistsAtURL)
|
let error = NSError(coreStoreErrorCode: .DifferentPersistentStoreExistsAtURL)
|
||||||
CoreStore.handleError(
|
CoreStore.handleError(
|
||||||
error,
|
error,
|
||||||
"Failed to add \"\(typeName(storage))\" 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
|
throw error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,9 +29,48 @@ import CoreData
|
|||||||
|
|
||||||
// MARK: - SetupResult
|
// MARK: - SetupResult
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
The `SetupResult` indicates the result of an asynchronous initialization of a persistent store.
|
||||||
|
The `SetupResult` can be treated as a boolean:
|
||||||
|
```
|
||||||
|
try! CoreStore.addStorage(
|
||||||
|
SQLiteStore(),
|
||||||
|
completion: { (result: SetupResult) -> Void in
|
||||||
|
if result {
|
||||||
|
// succeeded
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// failed
|
||||||
|
}
|
||||||
|
}n
|
||||||
|
)
|
||||||
|
```
|
||||||
|
or as an `enum`, where the resulting associated object can also be inspected:
|
||||||
|
```
|
||||||
|
try! CoreStore.addStorage(
|
||||||
|
SQLiteStore(),
|
||||||
|
completion: { (result: SetupResult) -> Void in
|
||||||
|
switch result {
|
||||||
|
case .Success(let storage):
|
||||||
|
// storage is the related StorageInterface instance
|
||||||
|
case .Failure(let error):
|
||||||
|
// error is the NSError instance for the failure
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
```
|
||||||
|
*/
|
||||||
public enum SetupResult<T: StorageInterface>: BooleanType {
|
public enum SetupResult<T: StorageInterface>: BooleanType {
|
||||||
|
|
||||||
|
/**
|
||||||
|
`SetupResult.Success` indicates that the storage setup succeeded. The associated object for this `enum` value is the related `StorageInterface` instance.
|
||||||
|
*/
|
||||||
case Success(T)
|
case Success(T)
|
||||||
|
|
||||||
|
/**
|
||||||
|
`SetupResult.Failure` indicates that the storage setup failed. The associated object for this value is the related `NSError` instance.
|
||||||
|
*/
|
||||||
case Failure(NSError)
|
case Failure(NSError)
|
||||||
|
|
||||||
|
|
||||||
@@ -71,46 +110,38 @@ public enum SetupResult<T: StorageInterface>: BooleanType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// MARK: - PersistentStoreResult
|
// MARK: - Deprecated
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
The `PersistentStoreResult` indicates the result of an asynchronous initialization of a persistent store.
|
Deprecated. Replaced by `SetupResult<T>` by using the new `addStorage(_:completion:)` method variants.
|
||||||
The `PersistentStoreResult` can be treated as a boolean:
|
|
||||||
```
|
|
||||||
try! CoreStore.addSQLiteStore(completion: { (result: PersistentStoreResult) -> Void in
|
|
||||||
if result {
|
|
||||||
// succeeded
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// failed
|
|
||||||
}
|
|
||||||
})
|
|
||||||
```
|
|
||||||
or as an `enum`, where the resulting associated object can also be inspected:
|
|
||||||
```
|
|
||||||
try! CoreStore.addSQLiteStore(completion: { (result: PersistentStoreResult) -> Void in
|
|
||||||
switch result {
|
|
||||||
case .Success(let persistentStore):
|
|
||||||
// persistentStore is the related NSPersistentStore instance
|
|
||||||
case .Failure(let error):
|
|
||||||
// error is the NSError instance for the failure
|
|
||||||
}
|
|
||||||
})
|
|
||||||
```
|
|
||||||
*/
|
*/
|
||||||
public enum PersistentStoreResult {
|
@available(*, deprecated=2.0.0, message="Replaced by SetupResult by using the new addStorage(_:completion:) method variants.")
|
||||||
|
public enum PersistentStoreResult: BooleanType {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
`PersistentStoreResult.Success` indicates that the persistent store process succeeded. The associated object for this `enum` value is the related `NSPersistentStore` instance.
|
Deprecated. Replaced by `SetupResult.Success` by using the new `addStorage(_:completion:)` method variants.
|
||||||
*/
|
*/
|
||||||
case Success(NSPersistentStore)
|
case Success(NSPersistentStore)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
`PersistentStoreResult.Failure` indicates that the persistent store process failed. The associated object for this value is the related `NSError` instance.
|
Deprecated. Replaced by `SetupResult.Failure` by using the new `addStorage(_:completion:)` method variants.
|
||||||
*/
|
*/
|
||||||
case Failure(NSError)
|
case Failure(NSError)
|
||||||
|
|
||||||
|
|
||||||
|
// MARK: BooleanType
|
||||||
|
|
||||||
|
public var boolValue: Bool {
|
||||||
|
|
||||||
|
switch self {
|
||||||
|
|
||||||
|
case .Success: return true
|
||||||
|
case .Failure: return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// MARK: Internal
|
// MARK: Internal
|
||||||
|
|
||||||
internal init(_ store: NSPersistentStore) {
|
internal init(_ store: NSPersistentStore) {
|
||||||
@@ -133,18 +164,3 @@ public enum PersistentStoreResult {
|
|||||||
self.init(NSError(coreStoreErrorCode: errorCode, userInfo: userInfo))
|
self.init(NSError(coreStoreErrorCode: errorCode, userInfo: userInfo))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// MARK: - PersistentStoreResult: BooleanType
|
|
||||||
|
|
||||||
extension PersistentStoreResult: BooleanType {
|
|
||||||
|
|
||||||
public var boolValue: Bool {
|
|
||||||
|
|
||||||
switch self {
|
|
||||||
|
|
||||||
case .Success: return true
|
|
||||||
case .Failure: return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user