mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-21 08:59:08 +01:00
WIP: Storage protocols
This commit is contained in:
@@ -123,30 +123,42 @@ 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 {
|
||||
|
||||
return try self.addStoreAndWait(InMemoryStore()).internalStore!
|
||||
}
|
||||
|
||||
/**
|
||||
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: the `NSPersistentStore` added to the stack.
|
||||
- parameter store: the `AtomicStore`.
|
||||
- returns: the `AtomicStore` added to the stack.
|
||||
*/
|
||||
public func addInMemoryStoreAndWait(configuration configuration: String? = nil) throws -> NSPersistentStore {
|
||||
public func addStoreAndWait<T: AtomicStore>(store: T) throws -> T {
|
||||
|
||||
CoreStore.assert(
|
||||
store.internalStore == nil,
|
||||
"The specified store was already added to the data stack:\n\(store)"
|
||||
)
|
||||
|
||||
do {
|
||||
|
||||
let store = try self.coordinator.addPersistentStoreSynchronously(
|
||||
NSInMemoryStoreType,
|
||||
configuration: configuration,
|
||||
URL: nil,
|
||||
let persistentStore = try self.coordinator.addPersistentStoreSynchronously(
|
||||
T.storeType,
|
||||
configuration: store.configuration,
|
||||
URL: store.storeURL,
|
||||
options: nil
|
||||
)
|
||||
self.updateMetadataForPersistentStore(store)
|
||||
self.updateMetadataForPersistentStore(persistentStore)
|
||||
store.internalStore = persistentStore
|
||||
return store
|
||||
}
|
||||
catch {
|
||||
|
||||
CoreStore.handleError(
|
||||
error as NSError,
|
||||
"Failed to add in-memory \(typeName(NSPersistentStore)) to the stack."
|
||||
"Failed to add \(typeName(T)) to the stack."
|
||||
)
|
||||
throw error
|
||||
}
|
||||
|
||||
48
CoreStore/Setting Up/PersistentStores/InMemoryStore.swift
Normal file
48
CoreStore/Setting Up/PersistentStores/InMemoryStore.swift
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// InMemoryStore.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: - InMemoryStore
|
||||
|
||||
public class InMemoryStore: AtomicStore {
|
||||
|
||||
public required init(configuration: String? = nil) {
|
||||
|
||||
self.configuration = configuration
|
||||
}
|
||||
|
||||
|
||||
// MARK: PersistentStore
|
||||
|
||||
public static let storeType = NSInMemoryStoreType
|
||||
|
||||
public let storeURL: NSURL? = nil
|
||||
|
||||
public let configuration: String?
|
||||
|
||||
public var internalStore: NSPersistentStore?
|
||||
}
|
||||
48
CoreStore/Setting Up/PersistentStores/Storage.swift
Normal file
48
CoreStore/Setting Up/PersistentStores/Storage.swift
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// Storage.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.
|
||||
//
|
||||
|
||||
|
||||
// MARK: - Storage
|
||||
|
||||
public protocol Storage: class {
|
||||
|
||||
static var storeType: String { get }
|
||||
|
||||
var storeURL: NSURL? { get }
|
||||
|
||||
var configuration: String? { get }
|
||||
|
||||
var internalStore: NSPersistentStore? { get set }
|
||||
}
|
||||
|
||||
|
||||
// MARK: - AtomicStore
|
||||
|
||||
public protocol AtomicStore: Storage { }
|
||||
|
||||
|
||||
// MARK: - IncrementalStore
|
||||
|
||||
public protocol IncrementalStore: Storage { }
|
||||
Reference in New Issue
Block a user