WIP: Storage protocol

This commit is contained in:
John Rommel Estropia
2016-03-02 08:02:33 +09:00
parent f71ad4c577
commit 99189d160f
12 changed files with 338 additions and 95 deletions

View File

@@ -59,14 +59,25 @@ public extension CoreStore {
}
/**
Adds an in-memory store to the `defaultStack`.
Creates a `Storage` of the specified store type with default values and adds it to the `defaultStack`. This method blocks until completion.
- parameter configuration: an optional configuration name from the model file. If not specified, defaults to `nil`.
- returns: the `NSPersistentStore` added to the stack.
- parameter storeType: the `Storage` type
- returns: the `Storage` added to the `defaultStack`
*/
public static func addInMemoryStoreAndWait(configuration configuration: String? = nil) throws -> NSPersistentStore {
public static func addStoreAndWait<T: Storage where T: DefaultInitializableStore>(storeType: T.Type) throws -> T {
return try self.defaultStack.addInMemoryStoreAndWait(configuration: configuration)
return try self.defaultStack.addStoreAndWait(storeType.init())
}
/**
Adds a `Storage` to the `defaultStack` and blocks until completion.
- parameter store: the `Storage`
- returns: the `Storage` added to the `defaultStack`
*/
public static func addStoreAndWait<T: Storage>(store: T) throws -> T {
return try self.defaultStack.addStoreAndWait(store)
}
/**
@@ -102,4 +113,13 @@ public extension CoreStore {
resetStoreOnModelMismatch: resetStoreOnModelMismatch
)
}
// MARK: Deprecated
@available(*, deprecated=2.0.0, message="Use addStoreAndWait(_:configuration:) by passing an InMemoryStore instance")
public static func addInMemoryStoreAndWait(configuration configuration: String? = nil) throws -> NSPersistentStore {
return try self.defaultStack.addInMemoryStoreAndWait(configuration: configuration)
}
}

View File

@@ -123,33 +123,33 @@ public final class DataStack {
return self.coordinator.managedObjectIDForURIRepresentation(url)
}
@available(*, deprecated=2.0.0, message="Use addStoreAndWait(_:configuration:) by passing an InMemoryStore instance")
public func addInMemoryStoreAndWait(configuration configuration: String? = nil) throws -> NSPersistentStore {
/**
Creates a `Storage` of the specified store type with default values and adds it to the stack. This method blocks until completion.
- parameter storeType: the `Storage` type
- returns: the `Storage` added to the stack
*/
public func addStoreAndWait<T: Storage where T: DefaultInitializableStore>(storeType: T.Type) throws -> T {
return try self.addStoreAndWait(InMemoryStore()).internalStore!
return try self.addStoreAndWait(storeType.init())
}
/**
Adds an in-memory store to the stack.
Adds a `Storage` to the stack and blocks until completion.
- parameter store: the `AtomicStore`.
- returns: the `AtomicStore` added to the stack.
- parameter store: the `Storage`
- returns: the `Storage` added to the stack
*/
public func addStoreAndWait<T: AtomicStore>(store: T) throws -> T {
public func addStoreAndWait<T: Storage>(store: T) throws -> T {
CoreStore.assert(
store.internalStore == nil,
"The specified store was already added to the data stack:\n\(store)"
"The specified store was already added to the data stack: \(store)"
)
do {
let persistentStore = try self.coordinator.addPersistentStoreSynchronously(
T.storeType,
configuration: store.configuration,
URL: store.storeURL,
options: nil
)
let persistentStore = try store.addToPersistentStoreCoordinatorSynchronously(self.coordinator)
self.updateMetadataForPersistentStore(persistentStore)
store.internalStore = persistentStore
return store
@@ -382,4 +382,13 @@ public final class DataStack {
let coordinator = self.coordinator
coordinator.persistentStores.forEach { _ = try? coordinator.removePersistentStore($0) }
}
// MARK: Deprecated
@available(*, deprecated=2.0.0, message="Use addStoreAndWait(_:configuration:) by passing an InMemoryStore instance")
public func addInMemoryStoreAndWait(configuration configuration: String? = nil) throws -> NSPersistentStore {
return try self.addStoreAndWait(InMemoryStore).internalStore!
}
}

View File

@@ -28,15 +28,23 @@ import CoreData
// MARK: - InMemoryStore
public class InMemoryStore: AtomicStore {
public class InMemoryStore: Storage, DefaultInitializableStore {
public required init(configuration: String? = nil) {
public required init(configuration: String?) {
self.configuration = configuration
}
// MARK: PersistentStore
// MARK: DefaultInitializableStore
public required init() {
self.configuration = nil
}
// MARK: Storage
public static let storeType = NSInMemoryStoreType
@@ -44,5 +52,7 @@ public class InMemoryStore: AtomicStore {
public let configuration: String?
public let storeOptions: [String: AnyObject]? = nil
public var internalStore: NSPersistentStore?
}

View File

@@ -0,0 +1,139 @@
//
// SQLiteStore.swift
// CoreStore
//
// Copyright © 2016 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import CoreData
// MARK: - SQLiteStore
public class SQLiteStore: Storage, DefaultInitializableStore {
public static let defaultRootDirectory: NSURL = {
#if os(tvOS)
let systemDirectorySearchPath = NSSearchPathDirectory.CachesDirectory
#else
let systemDirectorySearchPath = NSSearchPathDirectory.ApplicationSupportDirectory
#endif
let defaultSystemDirectory = NSFileManager
.defaultManager()
.URLsForDirectory(systemDirectorySearchPath, inDomains: .UserDomainMask).first!
return defaultSystemDirectory.URLByAppendingPathComponent(
NSBundle.mainBundle().bundleIdentifier ?? "com.CoreStore.DataStack",
isDirectory: true
)
}()
public static let defaultFileURL: NSURL = {
let applicationName = (NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as? String) ?? "CoreData"
return SQLiteStore.defaultRootDirectory
.URLByAppendingPathComponent(applicationName, isDirectory: false)
.URLByAppendingPathExtension("sqlite")
}()
public required init(fileURL: NSURL, configuration: String? = nil, resetStoreOnModelMismatch: Bool = false) {
self.fileURL = fileURL
self.configuration = configuration
self.resetStoreOnModelMismatch = resetStoreOnModelMismatch
}
public required init(fileName: String, configuration: String? = nil, resetStoreOnModelMismatch: Bool = false) {
self.fileURL = SQLiteStore.defaultRootDirectory
.URLByAppendingPathComponent(fileName, isDirectory: false)
self.configuration = configuration
self.resetStoreOnModelMismatch = resetStoreOnModelMismatch
}
// MARK: DefaultInitializableStore
public required init() {
self.fileURL = SQLiteStore.defaultFileURL
self.configuration = nil
self.resetStoreOnModelMismatch = false
}
// MARK: Storage
public static let storeType = NSSQLiteStoreType
public var storeURL: NSURL? {
return self.fileURL
}
public let configuration: String?
public let storeOptions: [String: AnyObject]? = [NSSQLitePragmasOption: ["journal_mode": "WAL"]]
public var internalStore: NSPersistentStore?
public func addToPersistentStoreCoordinatorSynchronously(coordinator: NSPersistentStoreCoordinator) throws -> NSPersistentStore {
let fileManager = NSFileManager.defaultManager()
do {
let fileURL = self.fileURL
try fileManager.createDirectoryAtURL(
fileURL.URLByDeletingLastPathComponent!,
withIntermediateDirectories: true,
attributes: nil
)
return try coordinator.addPersistentStoreSynchronously(
self.dynamicType.storeType,
configuration: self.configuration,
URL: fileURL,
options: self.storeOptions
)
}
catch let error as NSError where resetStoreOnModelMismatch && error.isCoreDataMigrationError {
fileManager.removeSQLiteStoreAtURL(fileURL)
return try coordinator.addPersistentStoreSynchronously(
self.dynamicType.storeType,
configuration: self.configuration,
URL: fileURL,
options: self.storeOptions
)
}
}
// MARK: Internal
internal let fileURL: NSURL
internal let resetStoreOnModelMismatch: Bool
}

View File

@@ -23,6 +23,8 @@
// SOFTWARE.
//
import CoreData
// MARK: - Storage
@@ -34,15 +36,54 @@ public protocol Storage: class {
var configuration: String? { get }
var storeOptions: [String: AnyObject]? { get }
var internalStore: NSPersistentStore? { get set }
func addToPersistentStoreCoordinatorSynchronously(coordinator: NSPersistentStoreCoordinator) throws -> NSPersistentStore
func addToPersistentStoreCoordinatorAsynchronously(coordinator: NSPersistentStoreCoordinator, completion: (NSPersistentStore) -> Void, failure: (NSError) -> Void) throws
}
public extension Storage {
public func addToPersistentStoreCoordinatorSynchronously(coordinator: NSPersistentStoreCoordinator) throws -> NSPersistentStore {
return try coordinator.addPersistentStoreSynchronously(
self.dynamicType.storeType,
configuration: self.configuration,
URL: self.storeURL,
options: self.storeOptions
)
}
public func addToPersistentStoreCoordinatorAsynchronously(coordinator: NSPersistentStoreCoordinator, completion: (NSPersistentStore) -> Void, failure: (NSError) -> Void) throws {
coordinator.performBlock {
do {
let persistentStore = try coordinator.addPersistentStoreWithType(
self.dynamicType.storeType,
configuration: self.configuration,
URL: self.storeURL,
options: self.storeOptions
)
completion(persistentStore)
}
catch {
failure(error as NSError)
}
}
}
}
// MARK: - AtomicStore
// MARK: - DefaultInitializableStore
public protocol AtomicStore: Storage { }
public protocol DefaultInitializableStore: Storage {
init()
}
// MARK: - IncrementalStore
public protocol IncrementalStore: Storage { }