mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-23 18:01:10 +01:00
WIP: clean up persistent store setup
This commit is contained in:
78
Sources/Internal/NSPersistentStore+Setup.swift
Normal file
78
Sources/Internal/NSPersistentStore+Setup.swift
Normal file
@@ -0,0 +1,78 @@
|
||||
//
|
||||
// NSPersistentStore+Setup.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 Foundation
|
||||
import CoreData
|
||||
|
||||
|
||||
// MARK: - NSPersistentStore
|
||||
|
||||
internal extension NSPersistentStore {
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
@nonobjc internal var storageInterface: StorageInterface? {
|
||||
|
||||
get {
|
||||
|
||||
let wrapper: StorageObject? = getAssociatedObjectForKey(
|
||||
&PropertyKeys.storageInterface,
|
||||
inObject: self
|
||||
)
|
||||
return wrapper?.storageInterface
|
||||
}
|
||||
set {
|
||||
|
||||
setAssociatedRetainedObject(
|
||||
StorageObject(newValue),
|
||||
forKey: &PropertyKeys.storageInterface,
|
||||
inObject: self
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private struct PropertyKeys {
|
||||
|
||||
static var storageInterface: Void?
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - StorageObject
|
||||
|
||||
private class StorageObject: NSObject {
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private let storageInterface: StorageInterface?
|
||||
|
||||
private init(_ storage: StorageInterface?) {
|
||||
|
||||
self.storageInterface = storage
|
||||
}
|
||||
}
|
||||
@@ -41,39 +41,20 @@ public extension DataStack {
|
||||
|
||||
public func addStorage<T: StorageInterface>(storage: T, completion: (SetupResult<T>) -> Void) throws -> NSProgress? {
|
||||
|
||||
// TODO: check
|
||||
let coordinator = self.coordinator;
|
||||
// if let persistentStore = coordinator.persistentStoreForURL(fileURL) {
|
||||
//
|
||||
// guard persistentStore.type == storage.dynamicType.storeType
|
||||
// && persistentStore.configurationName == (storage.configuration ?? Into.defaultConfigurationName) else {
|
||||
//
|
||||
// let error = NSError(coreStoreErrorCode: .DifferentPersistentStoreExistsAtURL)
|
||||
// CoreStore.handleError(
|
||||
// error,
|
||||
// "Failed to add SQLite \(typeName(storage)) at \"\(fileURL)\" because a different \(typeName(NSPersistentStore)) at that URL already exists."
|
||||
// )
|
||||
// throw error
|
||||
// }
|
||||
//
|
||||
// GCDQueue.Main.async {
|
||||
//
|
||||
// completion(SetupResult(storage))
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
|
||||
coordinator.performBlock {
|
||||
self.coordinator.performBlock {
|
||||
|
||||
if let _ = self.persistentStoreForStorage(storage) {
|
||||
|
||||
GCDQueue.Main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
do {
|
||||
|
||||
let persistentStore = try coordinator.addPersistentStoreWithType(
|
||||
storage.dynamicType.storeType,
|
||||
configuration: storage.configuration,
|
||||
URL: nil,
|
||||
options: storage.storeOptions
|
||||
)
|
||||
self.updateMetadataForStorage(storage, persistentStore: persistentStore)
|
||||
try self.createPersistentStoreFromStorage(storage, finalURL: nil)
|
||||
|
||||
GCDQueue.Main.async {
|
||||
|
||||
@@ -121,105 +102,113 @@ public extension DataStack {
|
||||
"The specified URL for the \(typeName(storage)) is invalid: \"\(fileURL)\""
|
||||
)
|
||||
|
||||
// TODO: check
|
||||
let coordinator = self.coordinator;
|
||||
if let persistentStore = coordinator.persistentStoreForURL(fileURL) {
|
||||
return try self.coordinator.performBlockAndWait {
|
||||
|
||||
guard persistentStore.type == storage.dynamicType.storeType
|
||||
&& persistentStore.configurationName == (storage.configuration ?? Into.defaultConfigurationName) else {
|
||||
|
||||
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."
|
||||
)
|
||||
throw error
|
||||
}
|
||||
|
||||
GCDQueue.Main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
let fileManager = NSFileManager.defaultManager()
|
||||
|
||||
do {
|
||||
|
||||
try fileManager.createDirectoryAtURL(
|
||||
fileURL.URLByDeletingLastPathComponent!,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(
|
||||
storage.dynamicType.storeType,
|
||||
URL: fileURL,
|
||||
options: storage.storeOptions
|
||||
)
|
||||
|
||||
return self.upgradeStorageIfNeeded(
|
||||
storage,
|
||||
metadata: metadata,
|
||||
completion: { (result) -> Void in
|
||||
|
||||
if case .Failure(let error) = result {
|
||||
|
||||
if storage.resetStoreOnModelMismatch && error.isCoreDataMigrationError {
|
||||
|
||||
do {
|
||||
|
||||
try _ = self.model[metadata].flatMap(storage.eraseStorageAndWait)
|
||||
try self.addStorageAndWait(storage)
|
||||
|
||||
GCDQueue.Main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
}
|
||||
catch {
|
||||
|
||||
completion(SetupResult(error as NSError))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
completion(SetupResult(error))
|
||||
return
|
||||
}
|
||||
|
||||
do {
|
||||
|
||||
try self.addStorageAndWait(storage)
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
catch {
|
||||
|
||||
completion(SetupResult(error as NSError))
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
catch let error as NSError
|
||||
where error.code == NSFileReadNoSuchFileError && error.domain == NSCocoaErrorDomain {
|
||||
|
||||
try self.addStorageAndWait(storage)
|
||||
if let _ = self.persistentStoreForStorage(storage) {
|
||||
|
||||
GCDQueue.Main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
catch {
|
||||
}
|
||||
|
||||
CoreStore.handleError(
|
||||
error as NSError,
|
||||
"Failed to load SQLite \(typeName(NSPersistentStore)) metadata."
|
||||
)
|
||||
throw error
|
||||
if let persistentStore = self.coordinator.persistentStoreForURL(fileURL) {
|
||||
|
||||
if let existingStorage = persistentStore.storageInterface as? T
|
||||
where storage.matchesPersistentStore(persistentStore) {
|
||||
|
||||
GCDQueue.Main.async {
|
||||
|
||||
completion(SetupResult(existingStorage))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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."
|
||||
)
|
||||
throw error
|
||||
}
|
||||
|
||||
do {
|
||||
|
||||
try NSFileManager.defaultManager().createDirectoryAtURL(
|
||||
fileURL.URLByDeletingLastPathComponent!,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(
|
||||
storage.dynamicType.storeType,
|
||||
URL: fileURL,
|
||||
options: storage.storeOptions
|
||||
)
|
||||
|
||||
return self.upgradeStorageIfNeeded(
|
||||
storage,
|
||||
metadata: metadata,
|
||||
completion: { (result) -> Void in
|
||||
|
||||
if case .Failure(let error) = result {
|
||||
|
||||
if storage.resetStoreOnModelMismatch && error.isCoreDataMigrationError {
|
||||
|
||||
do {
|
||||
|
||||
try _ = self.model[metadata].flatMap(storage.eraseStorageAndWait)
|
||||
try self.addStorageAndWait(storage)
|
||||
|
||||
GCDQueue.Main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
}
|
||||
catch {
|
||||
|
||||
completion(SetupResult(error as NSError))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
completion(SetupResult(error))
|
||||
return
|
||||
}
|
||||
|
||||
do {
|
||||
|
||||
try self.addStorageAndWait(storage)
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
catch {
|
||||
|
||||
completion(SetupResult(error as NSError))
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
catch let error as NSError
|
||||
where error.code == NSFileReadNoSuchFileError && error.domain == NSCocoaErrorDomain {
|
||||
|
||||
try self.addStorageAndWait(storage)
|
||||
|
||||
GCDQueue.Main.async {
|
||||
|
||||
completion(SetupResult(storage))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
catch {
|
||||
|
||||
CoreStore.handleError(
|
||||
error as NSError,
|
||||
"Failed to load SQLite \(typeName(NSPersistentStore)) metadata."
|
||||
)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,30 +223,36 @@ public extension DataStack {
|
||||
*/
|
||||
public func upgradeStorageIfNeeded<T: LocalStorage>(storage: T, completion: (MigrationResult) -> Void) throws -> NSProgress? {
|
||||
|
||||
let fileURL = storage.fileURL
|
||||
let metadata: [String: AnyObject]
|
||||
do {
|
||||
return try self.coordinator.performBlockAndWait {
|
||||
|
||||
metadata = try NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(
|
||||
storage.dynamicType.storeType,
|
||||
URL: fileURL,
|
||||
options: storage.storeOptions
|
||||
)
|
||||
let fileURL = storage.fileURL
|
||||
do {
|
||||
|
||||
CoreStore.assert(
|
||||
self.persistentStoreForStorage(storage) == nil,
|
||||
"Attempted to migrate an already added \(typeName(storage)) at URL \"\(fileURL)\""
|
||||
)
|
||||
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(
|
||||
storage.dynamicType.storeType,
|
||||
URL: fileURL,
|
||||
options: storage.storeOptions
|
||||
)
|
||||
return self.upgradeStorageIfNeeded(
|
||||
storage,
|
||||
metadata: metadata,
|
||||
completion: completion
|
||||
)
|
||||
}
|
||||
catch {
|
||||
|
||||
CoreStore.handleError(
|
||||
error as NSError,
|
||||
"Failed to load \(typeName(storage)) metadata from URL \"\(fileURL)\"."
|
||||
)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
catch {
|
||||
|
||||
CoreStore.handleError(
|
||||
error as NSError,
|
||||
"Failed to load \(typeName(storage)) metadata from URL \"\(fileURL)\"."
|
||||
)
|
||||
throw error
|
||||
}
|
||||
|
||||
return self.upgradeStorageIfNeeded(
|
||||
storage,
|
||||
metadata: metadata,
|
||||
completion: completion
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -272,36 +267,48 @@ public extension DataStack {
|
||||
@warn_unused_result
|
||||
public func requiredMigrationsForStorage<T: LocalStorage>(storage: T) throws -> [MigrationType] {
|
||||
|
||||
let fileURL = storage.fileURL
|
||||
let metadata: [String : AnyObject]
|
||||
do {
|
||||
return try self.coordinator.performBlockAndWait {
|
||||
|
||||
metadata = try NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(
|
||||
storage.dynamicType.storeType,
|
||||
URL: fileURL,
|
||||
options: storage.storeOptions
|
||||
)
|
||||
}
|
||||
catch {
|
||||
let fileURL = storage.fileURL
|
||||
|
||||
CoreStore.handleError(
|
||||
error as NSError,
|
||||
"Failed to load \(typeName(storage)) metadata from URL \"\(fileURL)\"."
|
||||
CoreStore.assert(
|
||||
self.persistentStoreForStorage(storage) == nil,
|
||||
"Attempted to query required migrations for an already added \(typeName(storage)) at URL \"\(fileURL)\""
|
||||
)
|
||||
throw error
|
||||
do {
|
||||
|
||||
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(
|
||||
storage.dynamicType.storeType,
|
||||
URL: fileURL,
|
||||
options: storage.storeOptions
|
||||
)
|
||||
|
||||
guard let migrationSteps = self.computeMigrationFromStorageMetadata(metadata, configuration: storage.configuration, mappingModelBundles: storage.mappingModelBundles) else {
|
||||
|
||||
let error = NSError(coreStoreErrorCode: .MappingModelNotFound)
|
||||
CoreStore.handleError(
|
||||
error,
|
||||
"Failed to find migration steps from the \(typeName(storage)) at URL \"\(fileURL)\" to version model \"\(self.modelVersion)\"."
|
||||
)
|
||||
throw error
|
||||
}
|
||||
|
||||
return migrationSteps.map { $0.migrationType }
|
||||
}
|
||||
catch let error as NSError
|
||||
where error.code == NSFileReadNoSuchFileError && error.domain == NSCocoaErrorDomain {
|
||||
|
||||
return []
|
||||
}
|
||||
catch {
|
||||
|
||||
CoreStore.handleError(
|
||||
error as NSError,
|
||||
"Failed to load \(typeName(storage)) metadata from URL \"\(fileURL)\"."
|
||||
)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
guard let migrationSteps = self.computeMigrationFromStorageMetadata(metadata, configuration: storage.configuration, mappingModelBundles: storage.mappingModelBundles) else {
|
||||
|
||||
let error = NSError(coreStoreErrorCode: .MappingModelNotFound)
|
||||
CoreStore.handleError(
|
||||
error,
|
||||
"Failed to find migration steps from the \(typeName(storage)) at URL \"\(fileURL)\" to version model \"\(self.modelVersion)\"."
|
||||
)
|
||||
throw error
|
||||
}
|
||||
|
||||
return migrationSteps.map { $0.migrationType }
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -120,7 +120,7 @@ public extension CoreStore {
|
||||
```
|
||||
|
||||
- parameter storage: the local storage
|
||||
- returns: the local storage added to the `defaultStack`
|
||||
- returns: the local storage added to the `defaultStack`. Note that this may not always be the same instance as the parameter argument if a previous `LocalStorage` was already added at the same URL and with the same configuration.
|
||||
*/
|
||||
public static func addStorageAndWait<T: LocalStorage>(storage: T) throws -> T {
|
||||
|
||||
|
||||
@@ -149,21 +149,18 @@ public final class DataStack {
|
||||
*/
|
||||
public func addStorageAndWait<T: StorageInterface>(storage: T) throws -> T {
|
||||
|
||||
CoreStore.assert(
|
||||
storage.internalStore == nil,
|
||||
"The specified \"\(typeName(storage))\" was already added to the data stack: \(storage)"
|
||||
)
|
||||
|
||||
do {
|
||||
|
||||
let persistentStore = try self.coordinator.addPersistentStoreSynchronously(
|
||||
storage.dynamicType.storeType,
|
||||
configuration: storage.configuration,
|
||||
URL: nil,
|
||||
options: storage.storeOptions
|
||||
)
|
||||
self.updateMetadataForStorage(storage, persistentStore: persistentStore)
|
||||
return storage
|
||||
return try self.coordinator.performBlockAndWait { () -> T in
|
||||
|
||||
if let _ = self.persistentStoreForStorage(storage) {
|
||||
|
||||
return storage
|
||||
}
|
||||
|
||||
try self.createPersistentStoreFromStorage(storage, finalURL: nil)
|
||||
return storage
|
||||
}
|
||||
}
|
||||
catch {
|
||||
|
||||
@@ -196,64 +193,50 @@ public final class DataStack {
|
||||
```
|
||||
|
||||
- parameter storage: the local storage
|
||||
- returns: the local storage added to the stack
|
||||
- returns: the local storage added to the stack. Note that this may not always be the same instance as the parameter argument if a previous `LocalStorage` was already added at the same URL and with the same configuration.
|
||||
*/
|
||||
public func addStorageAndWait<T: LocalStorage>(storage: T) throws -> T {
|
||||
|
||||
CoreStore.assert(
|
||||
storage.internalStore == nil,
|
||||
"The specified \"\(typeName(storage))\" was already added to the data stack: \(storage)"
|
||||
)
|
||||
|
||||
let fileURL = storage.fileURL
|
||||
CoreStore.assert(
|
||||
fileURL.fileURL,
|
||||
"The specified store URL for the \"\(typeName(storage))\" is invalid: \"\(fileURL)\""
|
||||
)
|
||||
|
||||
if let persistentStore = coordinator.persistentStoreForURL(fileURL) {
|
||||
|
||||
guard persistentStore.type == storage.dynamicType.storeType
|
||||
&& persistentStore.configurationName == (storage.configuration ?? Into.defaultConfigurationName) else {
|
||||
|
||||
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."
|
||||
)
|
||||
throw error
|
||||
return try self.coordinator.performBlockAndWait {
|
||||
|
||||
let fileURL = storage.fileURL
|
||||
CoreStore.assert(
|
||||
fileURL.fileURL,
|
||||
"The specified store URL for the \"\(typeName(storage))\" is invalid: \"\(fileURL)\""
|
||||
)
|
||||
|
||||
if let _ = self.persistentStoreForStorage(storage) {
|
||||
|
||||
return storage
|
||||
}
|
||||
|
||||
storage.internalStore = persistentStore
|
||||
return storage
|
||||
}
|
||||
|
||||
do {
|
||||
|
||||
let coordinator = self.coordinator
|
||||
return try coordinator.performBlockAndWait {
|
||||
if let persistentStore = self.coordinator.persistentStoreForURL(fileURL) {
|
||||
|
||||
let addStorage = { () throws -> T in
|
||||
if let existingStorage = persistentStore.storageInterface as? T
|
||||
where storage.matchesPersistentStore(persistentStore) {
|
||||
|
||||
let persistentStore = try coordinator.addPersistentStoreWithType(
|
||||
storage.dynamicType.storeType,
|
||||
configuration: storage.configuration,
|
||||
URL: fileURL,
|
||||
options: storage.storeOptions
|
||||
)
|
||||
self.updateMetadataForStorage(storage, persistentStore: persistentStore)
|
||||
return storage
|
||||
return existingStorage
|
||||
}
|
||||
|
||||
let fileManager = NSFileManager.defaultManager()
|
||||
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."
|
||||
)
|
||||
throw error
|
||||
}
|
||||
|
||||
do {
|
||||
|
||||
do {
|
||||
|
||||
try fileManager.createDirectoryAtURL(
|
||||
try NSFileManager.defaultManager().createDirectoryAtURL(
|
||||
fileURL.URLByDeletingLastPathComponent!,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
return try addStorage()
|
||||
try self.createPersistentStoreFromStorage(storage, finalURL: fileURL)
|
||||
return storage
|
||||
}
|
||||
catch let error as NSError where storage.resetStoreOnModelMismatch && error.isCoreDataMigrationError {
|
||||
|
||||
@@ -264,17 +247,18 @@ public final class DataStack {
|
||||
)
|
||||
try _ = self.model[metadata].flatMap(storage.eraseStorageAndWait)
|
||||
|
||||
return try addStorage()
|
||||
try self.createPersistentStoreFromStorage(storage, finalURL: fileURL)
|
||||
return storage
|
||||
}
|
||||
}
|
||||
}
|
||||
catch {
|
||||
|
||||
CoreStore.handleError(
|
||||
error as NSError,
|
||||
"Failed to add \(typeName(storage)) to the stack."
|
||||
)
|
||||
throw error
|
||||
catch {
|
||||
|
||||
CoreStore.handleError(
|
||||
error as NSError,
|
||||
"Failed to add \(typeName(storage)) to the stack."
|
||||
)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -300,6 +284,13 @@ public final class DataStack {
|
||||
return migrationQueue
|
||||
}()
|
||||
|
||||
internal func persistentStoreForStorage(storage: StorageInterface) -> NSPersistentStore? {
|
||||
|
||||
return self.coordinator.persistentStores
|
||||
.filter { $0.storageInterface === storage }
|
||||
.first
|
||||
}
|
||||
|
||||
internal func entityNameForEntityClass(entityClass: AnyClass) -> String? {
|
||||
|
||||
return self.model.entityNameForClass(entityClass)
|
||||
@@ -352,9 +343,15 @@ public final class DataStack {
|
||||
return returnValue
|
||||
}
|
||||
|
||||
internal func updateMetadataForStorage(storage: StorageInterface, persistentStore: NSPersistentStore) {
|
||||
internal func createPersistentStoreFromStorage(storage: StorageInterface, finalURL: NSURL?) throws -> NSPersistentStore {
|
||||
|
||||
storage.internalStore = persistentStore
|
||||
let persistentStore = try self.coordinator.addPersistentStoreWithType(
|
||||
storage.dynamicType.storeType,
|
||||
configuration: storage.configuration,
|
||||
URL: finalURL,
|
||||
options: storage.storeOptions
|
||||
)
|
||||
persistentStore.storageInterface = storage
|
||||
|
||||
self.storeMetadataUpdateQueue.barrierAsync {
|
||||
|
||||
@@ -375,6 +372,7 @@ public final class DataStack {
|
||||
self.entityConfigurationsMapping[managedObjectClassName]?.insert(configurationName)
|
||||
}
|
||||
}
|
||||
return persistentStore
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -29,7 +29,6 @@ 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:
|
||||
@@ -43,7 +42,7 @@ import CoreData
|
||||
else {
|
||||
// failed
|
||||
}
|
||||
}n
|
||||
}
|
||||
)
|
||||
```
|
||||
or as an `enum`, where the resulting associated object can also be inspected:
|
||||
@@ -114,18 +113,18 @@ public enum SetupResult<T: StorageInterface>: BooleanType {
|
||||
|
||||
|
||||
/**
|
||||
Deprecated. Replaced by `SetupResult<T>` by using the new `addStorage(_:completion:)` method variants.
|
||||
Deprecated. Replaced by `SetupResult<T>` when using the new `addStorage(_:completion:)` method variants.
|
||||
*/
|
||||
@available(*, deprecated=2.0.0, message="Replaced by SetupResult by using the new addStorage(_:completion:) method variants.")
|
||||
public enum PersistentStoreResult: BooleanType {
|
||||
|
||||
/**
|
||||
Deprecated. Replaced by `SetupResult.Success` by using the new `addStorage(_:completion:)` method variants.
|
||||
Deprecated. Replaced by `SetupResult.Success` when using the new `addStorage(_:completion:)` method variants.
|
||||
*/
|
||||
case Success(NSPersistentStore)
|
||||
|
||||
/**
|
||||
Deprecated. Replaced by `SetupResult.Failure` by using the new `addStorage(_:completion:)` method variants.
|
||||
Deprecated. Replaced by `SetupResult.Failure` when using the new `addStorage(_:completion:)` method variants.
|
||||
*/
|
||||
case Failure(NSError)
|
||||
|
||||
|
||||
@@ -34,8 +34,6 @@ public protocol StorageInterface: class {
|
||||
|
||||
var configuration: String? { get }
|
||||
var storeOptions: [String: AnyObject]? { get }
|
||||
|
||||
var internalStore: NSPersistentStore? { get set }
|
||||
}
|
||||
|
||||
|
||||
@@ -57,3 +55,16 @@ public protocol LocalStorage: StorageInterface {
|
||||
|
||||
func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel) throws
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal extension LocalStorage {
|
||||
|
||||
internal func matchesPersistentStore(persistentStore: NSPersistentStore) -> Bool {
|
||||
|
||||
return persistentStore.type == self.dynamicType.storeType
|
||||
&& persistentStore.configurationName == (self.configuration ?? Into.defaultConfigurationName)
|
||||
&& persistentStore.URL == self.fileURL
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user