From 2f8c100cb6bc3552b15ee93b10cb27d825749953 Mon Sep 17 00:00:00 2001 From: John Rommel Estropia Date: Tue, 8 Mar 2016 07:55:15 +0900 Subject: [PATCH] WIP: StorageInterface --- .../NSPersistentStoreCoordinator+Setup.swift | 4 ++-- CoreStore/Migrating/DataStack+Migration.swift | 14 ++++---------- CoreStore/Setting Up/DataStack.swift | 17 +++++++++++------ .../PersistentStores/SQLiteStore.swift | 4 ++-- .../PersistentStores/StorageInterface.swift | 2 +- .../MigrationsDemoViewController.swift | 2 +- 6 files changed, 21 insertions(+), 22 deletions(-) diff --git a/CoreStore/Internal/NSPersistentStoreCoordinator+Setup.swift b/CoreStore/Internal/NSPersistentStoreCoordinator+Setup.swift index 538d97e..d637ba8 100644 --- a/CoreStore/Internal/NSPersistentStoreCoordinator+Setup.swift +++ b/CoreStore/Internal/NSPersistentStoreCoordinator+Setup.swift @@ -33,7 +33,7 @@ internal extension NSPersistentStoreCoordinator { // MARK: Internal - internal func performBlockAndWait(block: () throws -> T) throws -> T { + @nonobjc internal func performBlockAndWait(block: () throws -> T) throws -> T { var result: T? var closureError: ErrorType? @@ -58,7 +58,7 @@ internal extension NSPersistentStoreCoordinator { throw closureError! } - internal func addPersistentStoreSynchronously(storeType: String, configuration: String?, URL storeURL: NSURL?, options: [NSObject : AnyObject]?) throws -> NSPersistentStore { + @nonobjc internal func addPersistentStoreSynchronously(storeType: String, configuration: String?, URL storeURL: NSURL?, options: [NSObject : AnyObject]?) throws -> NSPersistentStore { var store: NSPersistentStore? var storeError: NSError? diff --git a/CoreStore/Migrating/DataStack+Migration.swift b/CoreStore/Migrating/DataStack+Migration.swift index 36a98f1..5cf7a0d 100644 --- a/CoreStore/Migrating/DataStack+Migration.swift +++ b/CoreStore/Migrating/DataStack+Migration.swift @@ -73,8 +73,7 @@ public extension DataStack { URL: nil, options: storage.storeOptions ) - self.updateMetadataForPersistentStore(persistentStore) - storage.internalStore = persistentStore + self.updateMetadataForStorage(storage, persistentStore: persistentStore) GCDQueue.Main.async { @@ -147,7 +146,8 @@ public extension DataStack { let fileManager = NSFileManager.defaultManager() do { - _ = try? fileManager.createDirectoryAtURL( + + try fileManager.createDirectoryAtURL( fileURL.URLByDeletingLastPathComponent!, withIntermediateDirectories: true, attributes: nil @@ -168,9 +168,9 @@ public extension DataStack { if storage.resetStoreOnModelMismatch && error.isCoreDataMigrationError { - fileManager.removeSQLiteStoreAtURL(fileURL) do { + try _ = self.model[metadata].flatMap(storage.eraseStorageAndWait) try self.addStorageAndWait(storage) GCDQueue.Main.async { @@ -282,10 +282,6 @@ public extension DataStack { options: storage.storeOptions ) } - catch let error as NSError where error.code == NSFileReadNoSuchFileError && error.domain == NSCocoaErrorDomain { - - return [] - } catch { CoreStore.handleError( @@ -508,8 +504,6 @@ public extension DataStack { isDirectory: false ) - try storage.eraseStorageAndWait() - let migrationManager = MigrationManager( sourceModel: sourceModel, destinationModel: destinationModel, diff --git a/CoreStore/Setting Up/DataStack.swift b/CoreStore/Setting Up/DataStack.swift index 22485ad..cf35599 100644 --- a/CoreStore/Setting Up/DataStack.swift +++ b/CoreStore/Setting Up/DataStack.swift @@ -154,8 +154,7 @@ public final class DataStack { URL: nil, options: storage.storeOptions ) - self.updateMetadataForPersistentStore(persistentStore) - storage.internalStore = persistentStore + self.updateMetadataForStorage(storage, persistentStore: persistentStore) return storage } catch { @@ -237,7 +236,12 @@ public final class DataStack { } catch let error as NSError where storage.resetStoreOnModelMismatch && error.isCoreDataMigrationError { - try storage.eraseStorageAndWait() + let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStoreOfType( + storage.dynamicType.storeType, + URL: fileURL, + options: storage.storeOptions + ) + try _ = self.model[metadata].flatMap(storage.eraseStorageAndWait) return try coordinator.addPersistentStoreWithType( storage.dynamicType.storeType, @@ -247,8 +251,7 @@ public final class DataStack { ) } } - self.updateMetadataForPersistentStore(persistentStore) - storage.internalStore = persistentStore + self.updateMetadataForStorage(storage, persistentStore: persistentStore) return storage } catch { @@ -335,7 +338,9 @@ public final class DataStack { return returnValue } - internal func updateMetadataForPersistentStore(persistentStore: NSPersistentStore) { + internal func updateMetadataForStorage(storage: StorageInterface, persistentStore: NSPersistentStore) { + + storage.internalStore = persistentStore self.storeMetadataUpdateQueue.barrierAsync { diff --git a/CoreStore/Setting Up/PersistentStores/SQLiteStore.swift b/CoreStore/Setting Up/PersistentStores/SQLiteStore.swift index a28a71b..32f57bb 100644 --- a/CoreStore/Setting Up/PersistentStores/SQLiteStore.swift +++ b/CoreStore/Setting Up/PersistentStores/SQLiteStore.swift @@ -95,14 +95,14 @@ public class SQLiteStore: LocalStorage, DefaultInitializableStore { public var internalStore: NSPersistentStore? - public func eraseStorageAndWait() throws { + public func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel) throws { // TODO: check if attached to persistent store let fileURL = self.fileURL try autoreleasepool { - let journalUpdatingCoordinator = NSPersistentStoreCoordinator() + let journalUpdatingCoordinator = NSPersistentStoreCoordinator(managedObjectModel: soureModel) let store = try journalUpdatingCoordinator.addPersistentStoreWithType( self.dynamicType.storeType, configuration: self.configuration, diff --git a/CoreStore/Setting Up/PersistentStores/StorageInterface.swift b/CoreStore/Setting Up/PersistentStores/StorageInterface.swift index 9ed44e1..a2630bd 100644 --- a/CoreStore/Setting Up/PersistentStores/StorageInterface.swift +++ b/CoreStore/Setting Up/PersistentStores/StorageInterface.swift @@ -55,5 +55,5 @@ public protocol LocalStorage: StorageInterface { var mappingModelBundles: [NSBundle] { get } var resetStoreOnModelMismatch: Bool { get } - func eraseStorageAndWait() throws + func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel) throws } diff --git a/CoreStoreDemo/CoreStoreDemo/MIgrations Demo/MigrationsDemoViewController.swift b/CoreStoreDemo/CoreStoreDemo/MIgrations Demo/MigrationsDemoViewController.swift index 1bb848e..024a867 100644 --- a/CoreStoreDemo/CoreStoreDemo/MIgrations Demo/MigrationsDemoViewController.swift +++ b/CoreStoreDemo/CoreStoreDemo/MIgrations Demo/MigrationsDemoViewController.swift @@ -256,8 +256,8 @@ class MigrationsDemoViewController: UIViewController { else { self.segmentedControl?.selectedSegmentIndex = UISegmentedControlNoSegment - self._dataStack = nil self._listMonitor = nil + self._dataStack = nil } self.updateDisplay(reloadData: true, scrollToSelection: scrollToSelection, animated: false)