diff --git a/CoreStore.xcodeproj/project.pbxproj b/CoreStore.xcodeproj/project.pbxproj index ecbc595..b87829e 100644 --- a/CoreStore.xcodeproj/project.pbxproj +++ b/CoreStore.xcodeproj/project.pbxproj @@ -600,9 +600,9 @@ B5E84EDA1AFF84500064E85B /* Setting Up */ = { isa = PBXGroup; children = ( + B504D0D51B02362500B2BBB1 /* CoreStore+Setup.swift */, B5E84EDB1AFF84500064E85B /* DataStack.swift */, B5E84EDE1AFF84500064E85B /* SetupResult.swift */, - B504D0D51B02362500B2BBB1 /* CoreStore+Setup.swift */, B5FE4DA01C84818B00FA6A91 /* StorageInterfaces */, ); path = "Setting Up"; diff --git a/CoreStoreTests/CoreStoreTests.swift b/CoreStoreTests/CoreStoreTests.swift index 61b94bf..16aeff6 100644 --- a/CoreStoreTests/CoreStoreTests.swift +++ b/CoreStoreTests/CoreStoreTests.swift @@ -391,12 +391,7 @@ class CoreStoreTests: XCTestCase { do { - let defaultDirectory = NSFileManager.defaultManager().URLsForDirectory( - .ApplicationSupportDirectory, - inDomains: .UserDomainMask - ).first! - let fileManager = NSFileManager.defaultManager() - try fileManager.removeItemAtURL(defaultDirectory) + try NSFileManager.defaultManager().removeItemAtURL(SQLiteStore.defaultRootDirectory) } catch _ { } } diff --git a/Sources/Setting Up/CoreStore+Setup.swift b/Sources/Setting Up/CoreStore+Setup.swift index 55ef1fb..cc6d77d 100644 --- a/Sources/Setting Up/CoreStore+Setup.swift +++ b/Sources/Setting Up/CoreStore+Setup.swift @@ -58,8 +58,24 @@ public extension CoreStore { 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. + ``` + try CoreStore.addStorageAndWait(InMemoryStore) + ``` - parameter storeType: the `StorageInterface` type - returns: the `StorageInterface` added to the `defaultStack` @@ -71,6 +87,9 @@ public extension CoreStore { /** Adds a `StorageInterface` to the `defaultStack` and blocks until completion. + ``` + try CoreStore.addStorageAndWait(InMemoryStore(configuration: "Config1")) + ``` - parameter storage: the `StorageInterface` - 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. + ``` + try CoreStore.addStorageAndWait(SQLiteStore) + ``` - 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(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 - - returns: the local storage added to the stack + - returns: the local storage added to the `defaultStack` */ public static func addStorageAndWait(storage: T) throws -> T { diff --git a/Sources/Setting Up/DataStack.swift b/Sources/Setting Up/DataStack.swift index 620eeda..6288c6b 100644 --- a/Sources/Setting Up/DataStack.swift +++ b/Sources/Setting Up/DataStack.swift @@ -111,10 +111,23 @@ public final class DataStack { 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. ``` - try CoreStore.addStorageAndWait(InMemoryStore) + try dataStack.addStorageAndWait(InMemoryStore) ``` - parameter storeType: the `StorageInterface` type @@ -128,7 +141,7 @@ public final class DataStack { /** 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` @@ -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. ``` - try CoreStore.addStorageAndWait(SQLiteStore) + try dataStack.addStorageAndWait(SQLiteStore) ``` - parameter storeType: the `LocalStorageface` type @@ -179,7 +192,7 @@ public final class DataStack { /** 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 @@ -206,7 +219,7 @@ public final class DataStack { let error = NSError(coreStoreErrorCode: .DifferentPersistentStoreExistsAtURL) CoreStore.handleError( 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 } diff --git a/Sources/Setting Up/SetupResult.swift b/Sources/Setting Up/SetupResult.swift index e070f12..db50f83 100644 --- a/Sources/Setting Up/SetupResult.swift +++ b/Sources/Setting Up/SetupResult.swift @@ -29,9 +29,48 @@ import CoreData // 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: BooleanType { + /** + `SetupResult.Success` indicates that the storage setup succeeded. The associated object for this `enum` value is the related `StorageInterface` instance. + */ 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) @@ -71,46 +110,38 @@ public enum SetupResult: BooleanType { } -// MARK: - PersistentStoreResult +// MARK: - Deprecated + /** - The `PersistentStoreResult` indicates the result of an asynchronous initialization of a persistent store. - 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 - } - }) - ``` + Deprecated. Replaced by `SetupResult` by using the new `addStorage(_:completion:)` method variants. */ -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) /** - `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) + // MARK: BooleanType + + public var boolValue: Bool { + + switch self { + + case .Success: return true + case .Failure: return false + } + } + + // MARK: Internal internal init(_ store: NSPersistentStore) { @@ -133,18 +164,3 @@ public enum PersistentStoreResult { 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 - } - } -}