fix RecreateStoreOnModelMismatch option not working when an existing xcdatamodel gets updated without adding a new version

This commit is contained in:
John Rommel Estropia
2016-09-10 22:51:33 +09:00
parent 4a34012d58
commit f99d3cc21a
9 changed files with 36 additions and 12 deletions

View File

@@ -308,7 +308,7 @@ public protocol LocalStorage: StorageInterface {
var mappingModelBundles: [NSBundle] { get }
var localStorageOptions: LocalStorageOptions { get }
func storeOptionsForOptions(options: LocalStorageOptions) -> [String: AnyObject]?
func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel) throws
func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel?) throws
}
```
If you have custom `NSIncrementalStore` or `NSAtomicStore` subclasses, you can implement this protocol and use it similarly to `SQLiteStore`.

View File

@@ -223,7 +223,7 @@ public extension DataStack {
do {
try _ = self.model[metadata].flatMap(storage.eraseStorageAndWait)
_ = try storage.eraseStorageAndWait(soureModel: self.model[metadata])
try self.addStorageAndWait(storage)
GCDQueue.Main.async {
@@ -388,7 +388,7 @@ public extension DataStack {
URL: cacheFileURL,
options: storeOptions
)
try _ = self.model[metadata].flatMap(storage.eraseStorageAndWait)
_ = try storage.eraseStorageAndWait(soureModel: self.model[metadata])
try self.createPersistentStoreFromStorage(
storage,

View File

@@ -154,7 +154,7 @@ public final class CSSQLiteStore: NSObject, CSLocalStorage, CoreStoreObjectiveCT
Called by the `CSDataStack` to perform actual deletion of the store file from disk. Do not call directly! The `sourceModel` argument is a hint for the existing store's model version. For `CSSQLiteStore`, this converts the database's WAL journaling mode to DELETE before deleting the file.
*/
@objc
public func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel, error: NSErrorPointer) -> Bool {
public func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel?, error: NSErrorPointer) -> Bool {
return bridge(error) {

View File

@@ -121,5 +121,5 @@ public protocol CSLocalStorage: CSStorageInterface {
Called by the `CSDataStack` to perform actual deletion of the store file from disk. Do not call directly! The `sourceModel` argument is a hint for the existing store's model version. Implementers can use the `sourceModel` to perform necessary store operations. (SQLite stores for example, can convert WAL journaling mode to DELETE before deleting)
*/
@objc
func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel, error: NSErrorPointer) -> Bool
func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel?, error: NSErrorPointer) -> Bool
}

View File

@@ -263,7 +263,7 @@ public final class DataStack {
URL: fileURL,
options: storeOptions
)
try _ = self.model[metadata].flatMap(storage.eraseStorageAndWait)
_ = try storage.eraseStorageAndWait(soureModel: self.model[metadata])
try self.createPersistentStoreFromStorage(
storage,
@@ -359,7 +359,7 @@ public final class DataStack {
URL: cacheFileURL,
options: storeOptions
)
try _ = self.model[metadata].flatMap(storage.eraseStorageAndWait)
_ = try storage.eraseStorageAndWait(soureModel: self.model[metadata])
try self.createPersistentStoreFromStorage(
storage,

View File

@@ -424,11 +424,19 @@ public class ICloudStore: CloudStorage {
/**
Called by the `DataStack` to perform actual deletion of the store file from disk. Do not call directly! The `sourceModel` argument is a hint for the existing store's model version. For `SQLiteStore`, this converts the database's WAL journaling mode to DELETE before deleting the file.
*/
public func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel) throws {
public func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel?) throws {
// TODO: check if attached to persistent store
let cacheFileURL = self.cacheFileURL
guard let soureModel = soureModel else {
let fileManager = NSFileManager.defaultManager()
try fileManager.removeItemAtURL(cacheFileURL)
_ = try fileManager.removeItemAtPath("\(cacheFileURL.absoluteString)-wal")
_ = try fileManager.removeItemAtPath("\(cacheFileURL.absoluteString)-shm")
return
}
try cs_autoreleasepool {
let journalUpdatingCoordinator = NSPersistentStoreCoordinator(managedObjectModel: soureModel)

View File

@@ -165,11 +165,19 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
/**
Called by the `DataStack` to perform actual deletion of the store file from disk. Do not call directly! The `sourceModel` argument is a hint for the existing store's model version. For `SQLiteStore`, this converts the database's WAL journaling mode to DELETE before deleting the file.
*/
public func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel) throws {
public func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel?) throws {
// TODO: check if attached to persistent store
let fileURL = self.fileURL
guard let soureModel = soureModel else {
let fileManager = NSFileManager.defaultManager()
try fileManager.removeItemAtURL(fileURL)
_ = try fileManager.removeItemAtPath("\(fileURL.absoluteString)-wal")
_ = try fileManager.removeItemAtPath("\(fileURL.absoluteString)-shm")
return
}
try cs_autoreleasepool {
let journalUpdatingCoordinator = NSPersistentStoreCoordinator(managedObjectModel: soureModel)

View File

@@ -162,11 +162,19 @@ public final class SQLiteStore: LocalStorage, DefaultInitializableStore {
/**
Called by the `DataStack` to perform actual deletion of the store file from disk. Do not call directly! The `sourceModel` argument is a hint for the existing store's model version. For `SQLiteStore`, this converts the database's WAL journaling mode to DELETE before deleting the file.
*/
public func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel) throws {
public func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel?) throws {
// TODO: check if attached to persistent store
let fileURL = self.fileURL
guard let soureModel = soureModel else {
let fileManager = NSFileManager.defaultManager()
try fileManager.removeItemAtURL(fileURL)
_ = try fileManager.removeItemAtPath("\(fileURL.absoluteString)-wal")
_ = try fileManager.removeItemAtPath("\(fileURL.absoluteString)-shm")
return
}
try cs_autoreleasepool {
let journalUpdatingCoordinator = NSPersistentStoreCoordinator(managedObjectModel: soureModel)

View File

@@ -158,7 +158,7 @@ public protocol LocalStorage: StorageInterface {
/**
Called by the `DataStack` to perform actual deletion of the store file from disk. **Do not call directly!** The `sourceModel` argument is a hint for the existing store's model version. Implementers can use the `sourceModel` to perform necessary store operations. (SQLite stores for example, can convert WAL journaling mode to DELETE before deleting)
*/
func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel) throws
func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel?) throws
}
internal extension LocalStorage {
@@ -242,7 +242,7 @@ public protocol CloudStorage: StorageInterface {
/**
Called by the `DataStack` to perform actual deletion of the store file from disk. **Do not call directly!** The `sourceModel` argument is a hint for the existing store's model version. Implementers can use the `sourceModel` to perform necessary store operations. (Cloud stores for example, can set the NSPersistentStoreRemoveUbiquitousMetadataOption option before deleting)
*/
func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel) throws
func eraseStorageAndWait(soureModel soureModel: NSManagedObjectModel?) throws
}
internal extension CloudStorage {