mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-24 10:21:17 +01:00
WIP: segfault
This commit is contained in:
@@ -45,7 +45,7 @@ public class ICloudStore: CloudStorage {
|
||||
ubiquitousContainerID: "iCloud.com.mycompany.myapp.containername",
|
||||
ubiquitousPeerToken: "9614d658014f4151a95d8048fb717cf0",
|
||||
configuration: "Config1",
|
||||
cloudStorageOptions: .RecreateLocalStoreOnModelMismatch
|
||||
cloudStorageOptions: .recreateLocalStoreOnModelMismatch
|
||||
) else {
|
||||
// iCloud is not available on the device
|
||||
return
|
||||
@@ -73,7 +73,7 @@ public class ICloudStore: CloudStorage {
|
||||
"The ubiquitousContentName cannot be empty."
|
||||
)
|
||||
CoreStore.assert(
|
||||
!ubiquitousContentName.containsString("."),
|
||||
!ubiquitousContentName.contains("."),
|
||||
"The ubiquitousContentName cannot contain periods."
|
||||
)
|
||||
CoreStore.assert(
|
||||
@@ -85,8 +85,8 @@ public class ICloudStore: CloudStorage {
|
||||
"The ubiquitousPeerToken should not be empty if provided."
|
||||
)
|
||||
|
||||
let fileManager = NSFileManager.defaultManager()
|
||||
guard let cacheFileURL = fileManager.URLForUbiquityContainerIdentifier(ubiquitousContainerID) else {
|
||||
let fileManager = FileManager.default
|
||||
guard let cacheFileURL = fileManager.urlForUbiquityContainerIdentifier(ubiquitousContainerID) else {
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -110,10 +110,10 @@ public class ICloudStore: CloudStorage {
|
||||
|
||||
- parameter observer: the observer to start sending ubiquitous notifications to
|
||||
*/
|
||||
public func addObserver<T: ICloudStoreObserver>(observer: T) {
|
||||
public func addObserver<T: ICloudStoreObserver>(_ observer: T) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to add an observer of type \(cs_typeName(observer)) outside the main thread."
|
||||
)
|
||||
|
||||
@@ -198,10 +198,10 @@ public class ICloudStore: CloudStorage {
|
||||
|
||||
- parameter observer: the observer to stop sending ubiquitous notifications to
|
||||
*/
|
||||
public func removeObserver(observer: ICloudStoreObserver) {
|
||||
public func removeObserver(_ observer: ICloudStoreObserver) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
Thread.isMainThread,
|
||||
"Attempted to remove an observer of type \(cs_typeName(observer)) outside the main thread."
|
||||
)
|
||||
let nilValue: AnyObject? = nil
|
||||
@@ -271,7 +271,7 @@ public class ICloudStore: CloudStorage {
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
public func didAddToDataStack(dataStack: DataStack) {
|
||||
public func didAddToDataStack(_ dataStack: DataStack) {
|
||||
|
||||
self.didRemoveFromDataStack(dataStack)
|
||||
|
||||
@@ -280,38 +280,38 @@ public class ICloudStore: CloudStorage {
|
||||
|
||||
cs_setAssociatedRetainedObject(
|
||||
NotificationObserver(
|
||||
notificationName: NSPersistentStoreCoordinatorStoresWillChangeNotification,
|
||||
notificationName: NSNotification.Name.NSPersistentStoreCoordinatorStoresWillChange.rawValue,
|
||||
object: coordinator,
|
||||
closure: { [weak self, weak dataStack] (note) -> Void in
|
||||
|
||||
guard let `self` = self,
|
||||
let dataStack = dataStack,
|
||||
let userInfo = note.userInfo,
|
||||
let userInfo = (note as NSNotification).userInfo,
|
||||
let transitionType = userInfo[NSPersistentStoreUbiquitousTransitionTypeKey] as? NSNumber else {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
let notification: String
|
||||
switch NSPersistentStoreUbiquitousTransitionType(rawValue: transitionType.unsignedIntegerValue) {
|
||||
switch NSPersistentStoreUbiquitousTransitionType(rawValue: transitionType.uintValue) {
|
||||
|
||||
case .InitialImportCompleted?:
|
||||
case .initialImportCompleted?:
|
||||
notification = ICloudUbiquitousStoreWillFinishInitialImportNotification
|
||||
|
||||
case .AccountAdded?:
|
||||
case .accountAdded?:
|
||||
notification = ICloudUbiquitousStoreWillAddAccountNotification
|
||||
|
||||
case .AccountRemoved?:
|
||||
case .accountRemoved?:
|
||||
notification = ICloudUbiquitousStoreWillRemoveAccountNotification
|
||||
|
||||
case .ContentRemoved?:
|
||||
case .contentRemoved?:
|
||||
notification = ICloudUbiquitousStoreWillRemoveContentNotification
|
||||
|
||||
default:
|
||||
return
|
||||
}
|
||||
NSNotificationCenter.defaultCenter().postNotificationName(
|
||||
notification,
|
||||
NotificationCenter.default.post(
|
||||
name: Notification.Name(rawValue: notification),
|
||||
object: self,
|
||||
userInfo: [UserInfoKeyDataStack: dataStack]
|
||||
)
|
||||
@@ -322,38 +322,38 @@ public class ICloudStore: CloudStorage {
|
||||
)
|
||||
cs_setAssociatedRetainedObject(
|
||||
NotificationObserver(
|
||||
notificationName: NSPersistentStoreCoordinatorStoresDidChangeNotification,
|
||||
notificationName: NSNotification.Name.NSPersistentStoreCoordinatorStoresDidChange.rawValue,
|
||||
object: coordinator,
|
||||
closure: { [weak self, weak dataStack] (note) -> Void in
|
||||
|
||||
guard let `self` = self,
|
||||
let dataStack = dataStack,
|
||||
let userInfo = note.userInfo,
|
||||
let userInfo = (note as NSNotification).userInfo,
|
||||
let transitionType = userInfo[NSPersistentStoreUbiquitousTransitionTypeKey] as? NSNumber else {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
let notification: String
|
||||
switch NSPersistentStoreUbiquitousTransitionType(rawValue: transitionType.unsignedIntegerValue) {
|
||||
switch NSPersistentStoreUbiquitousTransitionType(rawValue: transitionType.uintValue) {
|
||||
|
||||
case .InitialImportCompleted?:
|
||||
case .initialImportCompleted?:
|
||||
notification = ICloudUbiquitousStoreDidFinishInitialImportNotification
|
||||
|
||||
case .AccountAdded?:
|
||||
case .accountAdded?:
|
||||
notification = ICloudUbiquitousStoreDidAddAccountNotification
|
||||
|
||||
case .AccountRemoved?:
|
||||
case .accountRemoved?:
|
||||
notification = ICloudUbiquitousStoreDidRemoveAccountNotification
|
||||
|
||||
case .ContentRemoved?:
|
||||
case .contentRemoved?:
|
||||
notification = ICloudUbiquitousStoreDidRemoveContentNotification
|
||||
|
||||
default:
|
||||
return
|
||||
}
|
||||
NSNotificationCenter.defaultCenter().postNotificationName(
|
||||
notification,
|
||||
NotificationCenter.default.post(
|
||||
name: Notification.Name(rawValue: notification),
|
||||
object: self,
|
||||
userInfo: [UserInfoKeyDataStack: dataStack]
|
||||
)
|
||||
@@ -367,7 +367,7 @@ public class ICloudStore: CloudStorage {
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
public func didRemoveFromDataStack(dataStack: DataStack) {
|
||||
public func didRemoveFromDataStack(_ dataStack: DataStack) {
|
||||
|
||||
let coordinator = dataStack.coordinator
|
||||
let nilValue: AnyObject? = nil
|
||||
@@ -391,7 +391,7 @@ public class ICloudStore: CloudStorage {
|
||||
/**
|
||||
The `NSURL` that points to the ubiquity container file
|
||||
*/
|
||||
public let cacheFileURL: NSURL
|
||||
public let cacheFileURL: URL
|
||||
|
||||
/**
|
||||
Options that tell the `DataStack` how to setup the persistent store
|
||||
@@ -401,20 +401,20 @@ public class ICloudStore: CloudStorage {
|
||||
/**
|
||||
The options dictionary for the specified `CloudStorageOptions`
|
||||
*/
|
||||
public func storeOptionsForOptions(options: CloudStorageOptions) -> [String: AnyObject]? {
|
||||
public func storeOptionsForOptions(_ options: CloudStorageOptions) -> [String: AnyObject]? {
|
||||
|
||||
if options == .None {
|
||||
if options == .none {
|
||||
|
||||
return self.storeOptions
|
||||
}
|
||||
|
||||
var storeOptions = self.storeOptions ?? [:]
|
||||
if options.contains(.AllowSynchronousLightweightMigration) {
|
||||
if options.contains(.allowSynchronousLightweightMigration) {
|
||||
|
||||
storeOptions[NSMigratePersistentStoresAutomaticallyOption] = true
|
||||
storeOptions[NSInferMappingModelAutomaticallyOption] = true
|
||||
}
|
||||
if options.contains(.RecreateLocalStoreOnModelMismatch) {
|
||||
if options.contains(.recreateLocalStoreOnModelMismatch) {
|
||||
|
||||
storeOptions[NSPersistentStoreRebuildFromUbiquitousContentOption] = true
|
||||
}
|
||||
@@ -424,7 +424,7 @@ 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: NSManagedObjectModel) throws {
|
||||
|
||||
// TODO: check if attached to persistent store
|
||||
|
||||
@@ -436,18 +436,18 @@ public class ICloudStore: CloudStorage {
|
||||
NSSQLitePragmasOption: ["journal_mode": "DELETE"],
|
||||
NSPersistentStoreRemoveUbiquitousMetadataOption: true
|
||||
]
|
||||
let store = try journalUpdatingCoordinator.addPersistentStoreWithType(
|
||||
self.dynamicType.storeType,
|
||||
configuration: self.configuration,
|
||||
URL: cacheFileURL,
|
||||
let store = try journalUpdatingCoordinator.addPersistentStore(
|
||||
ofType: self.dynamicType.storeType,
|
||||
configurationName: self.configuration,
|
||||
at: cacheFileURL,
|
||||
options: options
|
||||
)
|
||||
try journalUpdatingCoordinator.removePersistentStore(store)
|
||||
try NSPersistentStoreCoordinator.removeUbiquitousContentAndPersistentStoreAtURL(
|
||||
cacheFileURL,
|
||||
try journalUpdatingCoordinator.remove(store)
|
||||
try NSPersistentStoreCoordinator.removeUbiquitousContentAndPersistentStore(
|
||||
at: cacheFileURL,
|
||||
options: options
|
||||
)
|
||||
try NSFileManager.defaultManager().removeItemAtURL(cacheFileURL)
|
||||
try FileManager.default.removeItem(at: cacheFileURL)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -471,7 +471,7 @@ public class ICloudStore: CloudStorage {
|
||||
|
||||
private weak var dataStack: DataStack?
|
||||
|
||||
private func registerNotification<T: ICloudStoreObserver>(notificationKey: UnsafePointer<Void>, name: String, toObserver observer: T, callback: (observer: T, storage: ICloudStore, dataStack: DataStack) -> Void) {
|
||||
private func registerNotification<T: ICloudStoreObserver>(_ notificationKey: UnsafePointer<Void>, name: String, toObserver observer: T, callback: (observer: T, storage: ICloudStore, dataStack: DataStack) -> Void) {
|
||||
|
||||
cs_setAssociatedRetainedObject(
|
||||
NotificationObserver(
|
||||
@@ -481,7 +481,7 @@ public class ICloudStore: CloudStorage {
|
||||
|
||||
guard let `self` = self,
|
||||
let observer = observer,
|
||||
let dataStack = note.userInfo?[UserInfoKeyDataStack] as? DataStack
|
||||
let dataStack = (note as NSNotification).userInfo?[UserInfoKeyDataStack] as? DataStack
|
||||
where self.dataStack === dataStack else {
|
||||
|
||||
return
|
||||
|
||||
@@ -45,7 +45,7 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreWillFinishUbiquitousStoreInitialImport(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreWillFinishUbiquitousStoreInitialImport(storage: ICloudStore, dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Notifies that the initial ubiquitous store import completed
|
||||
@@ -53,7 +53,7 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreDidFinishUbiquitousStoreInitialImport(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreDidFinishUbiquitousStoreInitialImport(storage: ICloudStore, dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Notifies that an iCloud account will be added to the coordinator
|
||||
@@ -61,7 +61,7 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreWillAddAccount(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreWillAddAccount(storage: ICloudStore, dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Notifies that an iCloud account was added to the coordinator
|
||||
@@ -69,7 +69,7 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreDidAddAccount(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreDidAddAccount(storage: ICloudStore, dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Notifies that an iCloud account will be removed from the coordinator
|
||||
@@ -77,7 +77,7 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreWillRemoveAccount(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreWillRemoveAccount(storage: ICloudStore, dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Notifies that an iCloud account was removed from the coordinator
|
||||
@@ -85,7 +85,7 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreDidRemoveAccount(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreDidRemoveAccount(storage: ICloudStore, dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Notifies that iCloud contents will be deleted
|
||||
@@ -93,7 +93,7 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreWillRemoveContent(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreWillRemoveContent(storage: ICloudStore, dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Notifies that iCloud contents were deleted
|
||||
@@ -101,26 +101,26 @@ public protocol ICloudStoreObserver: class {
|
||||
- parameter storage: the `ICloudStore` instance being observed
|
||||
- parameter dataStack: the `DataStack` that manages the peristent store
|
||||
*/
|
||||
func iCloudStoreDidRemoveContent(storage storage: ICloudStore, dataStack: DataStack)
|
||||
func iCloudStoreDidRemoveContent(storage: ICloudStore, dataStack: DataStack)
|
||||
}
|
||||
|
||||
public extension ICloudStoreObserver {
|
||||
|
||||
public func iCloudStoreWillFinishUbiquitousStoreInitialImport(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreWillFinishUbiquitousStoreInitialImport(storage: ICloudStore, dataStack: DataStack) {}
|
||||
|
||||
public func iCloudStoreDidFinishUbiquitousStoreInitialImport(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreDidFinishUbiquitousStoreInitialImport(storage: ICloudStore, dataStack: DataStack) {}
|
||||
|
||||
public func iCloudStoreWillAddAccount(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreWillAddAccount(storage: ICloudStore, dataStack: DataStack) {}
|
||||
|
||||
public func iCloudStoreDidAddAccount(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreDidAddAccount(storage: ICloudStore, dataStack: DataStack) {}
|
||||
|
||||
public func iCloudStoreWillRemoveAccount(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreWillRemoveAccount(storage: ICloudStore, dataStack: DataStack) {}
|
||||
|
||||
public func iCloudStoreDidRemoveAccount(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreDidRemoveAccount(storage: ICloudStore, dataStack: DataStack) {}
|
||||
|
||||
public func iCloudStoreWillRemoveContent(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreWillRemoveContent(storage: ICloudStore, dataStack: DataStack) {}
|
||||
|
||||
public func iCloudStoreDidRemoveContent(storage storage: ICloudStore, dataStack: DataStack) {}
|
||||
public func iCloudStoreDidRemoveContent(storage: ICloudStore, dataStack: DataStack) {}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -74,7 +74,7 @@ public final class InMemoryStore: StorageInterface, DefaultInitializableStore {
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
public func didAddToDataStack(dataStack: DataStack) {
|
||||
public func didAddToDataStack(_ dataStack: DataStack) {
|
||||
|
||||
self.dataStack = dataStack
|
||||
}
|
||||
@@ -82,7 +82,7 @@ public final class InMemoryStore: StorageInterface, DefaultInitializableStore {
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
public func didRemoveFromDataStack(dataStack: DataStack) {
|
||||
public func didRemoveFromDataStack(_ dataStack: DataStack) {
|
||||
|
||||
self.dataStack = nil
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
- parameter mappingModelBundles: a list of `NSBundle`s from which to search mapping models for migration.
|
||||
- parameter localStorageOptions: When the `SQLiteStore` is passed to the `DataStack`'s `addStorage()` methods, tells the `DataStack` how to setup the persistent store. Defaults to `.None`.
|
||||
*/
|
||||
public init(fileURL: NSURL, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), localStorageOptions: LocalStorageOptions = nil) {
|
||||
public init(fileURL: URL, configuration: String? = nil, mappingModelBundles: [Bundle] = Bundle.allBundles, localStorageOptions: LocalStorageOptions = nil) {
|
||||
|
||||
self.fileURL = fileURL
|
||||
self.configuration = configuration
|
||||
@@ -61,9 +61,9 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
- parameter mappingModelBundles: a list of `NSBundle`s from which to search mapping models for migration.
|
||||
- parameter localStorageOptions: When the `SQLiteStore` is passed to the `DataStack`'s `addStorage()` methods, tells the `DataStack` how to setup the persistent store. Defaults to `.None`.
|
||||
*/
|
||||
public init(fileName: String, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), localStorageOptions: LocalStorageOptions = nil) {
|
||||
public init(fileName: String, configuration: String? = nil, mappingModelBundles: [Bundle] = Bundle.allBundles, localStorageOptions: LocalStorageOptions = nil) {
|
||||
|
||||
self.fileURL = LegacySQLiteStore.defaultRootDirectory.URLByAppendingPathComponent(
|
||||
self.fileURL = try! LegacySQLiteStore.defaultRootDirectory.appendingPathComponent(
|
||||
fileName,
|
||||
isDirectory: false
|
||||
)
|
||||
@@ -84,7 +84,7 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
|
||||
self.fileURL = LegacySQLiteStore.defaultFileURL
|
||||
self.configuration = nil
|
||||
self.mappingModelBundles = NSBundle.allBundles()
|
||||
self.mappingModelBundles = Bundle.allBundles
|
||||
self.localStorageOptions = nil
|
||||
}
|
||||
|
||||
@@ -99,15 +99,15 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
/**
|
||||
The options dictionary for the specified `LocalStorageOptions`
|
||||
*/
|
||||
public func storeOptionsForOptions(options: LocalStorageOptions) -> [String: AnyObject]? {
|
||||
public func storeOptionsForOptions(_ options: LocalStorageOptions) -> [String: AnyObject]? {
|
||||
|
||||
if options == .None {
|
||||
if options == .none {
|
||||
|
||||
return self.storeOptions
|
||||
}
|
||||
|
||||
var storeOptions = self.storeOptions ?? [:]
|
||||
if options.contains(.AllowSynchronousLightweightMigration) {
|
||||
if options.contains(.allowSynchronousLightweightMigration) {
|
||||
|
||||
storeOptions[NSMigratePersistentStoresAutomaticallyOption] = true
|
||||
storeOptions[NSInferMappingModelAutomaticallyOption] = true
|
||||
@@ -131,7 +131,7 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
public func didAddToDataStack(dataStack: DataStack) {
|
||||
public func didAddToDataStack(_ dataStack: DataStack) {
|
||||
|
||||
self.dataStack = dataStack
|
||||
}
|
||||
@@ -139,7 +139,7 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
public func didRemoveFromDataStack(dataStack: DataStack) {
|
||||
public func didRemoveFromDataStack(_ dataStack: DataStack) {
|
||||
|
||||
self.dataStack = nil
|
||||
}
|
||||
@@ -150,12 +150,12 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
/**
|
||||
The `NSURL` that points to the SQLite file
|
||||
*/
|
||||
public let fileURL: NSURL
|
||||
public let fileURL: URL
|
||||
|
||||
/**
|
||||
The `NSBundle`s from which to search mapping models for migrations
|
||||
*/
|
||||
public let mappingModelBundles: [NSBundle]
|
||||
public let mappingModelBundles: [Bundle]
|
||||
|
||||
/**
|
||||
Options that tell the `DataStack` how to setup the persistent store
|
||||
@@ -165,7 +165,7 @@ 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: NSManagedObjectModel) throws {
|
||||
|
||||
// TODO: check if attached to persistent store
|
||||
|
||||
@@ -173,37 +173,37 @@ public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
try cs_autoreleasepool {
|
||||
|
||||
let journalUpdatingCoordinator = NSPersistentStoreCoordinator(managedObjectModel: soureModel)
|
||||
let store = try journalUpdatingCoordinator.addPersistentStoreWithType(
|
||||
self.dynamicType.storeType,
|
||||
configuration: self.configuration,
|
||||
URL: fileURL,
|
||||
let store = try journalUpdatingCoordinator.addPersistentStore(
|
||||
ofType: self.dynamicType.storeType,
|
||||
configurationName: self.configuration,
|
||||
at: fileURL,
|
||||
options: [NSSQLitePragmasOption: ["journal_mode": "DELETE"]]
|
||||
)
|
||||
try journalUpdatingCoordinator.removePersistentStore(store)
|
||||
try NSFileManager.defaultManager().removeItemAtURL(fileURL)
|
||||
try journalUpdatingCoordinator.remove(store)
|
||||
try FileManager.default.removeItem(at: fileURL)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal static let defaultRootDirectory: NSURL = {
|
||||
internal static let defaultRootDirectory: URL = {
|
||||
|
||||
#if os(tvOS)
|
||||
let systemDirectorySearchPath = NSSearchPathDirectory.CachesDirectory
|
||||
#else
|
||||
let systemDirectorySearchPath = NSSearchPathDirectory.ApplicationSupportDirectory
|
||||
let systemDirectorySearchPath = FileManager.SearchPathDirectory.applicationSupportDirectory
|
||||
#endif
|
||||
|
||||
return NSFileManager.defaultManager().URLsForDirectory(
|
||||
return FileManager.default.urlsForDirectory(
|
||||
systemDirectorySearchPath,
|
||||
inDomains: .UserDomainMask
|
||||
inDomains: .userDomainMask
|
||||
).first!
|
||||
}()
|
||||
|
||||
internal static let defaultFileURL = LegacySQLiteStore.defaultRootDirectory
|
||||
.URLByAppendingPathComponent(DataStack.applicationName, isDirectory: false)
|
||||
.URLByAppendingPathExtension("sqlite")
|
||||
internal static let defaultFileURL = try! LegacySQLiteStore.defaultRootDirectory
|
||||
.appendingPathComponent(DataStack.applicationName, isDirectory: false)
|
||||
.appendingPathExtension("sqlite")
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
@@ -43,7 +43,7 @@ public final class SQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
- parameter mappingModelBundles: a list of `NSBundle`s from which to search mapping models (*.xcmappingmodel) for migration.
|
||||
- parameter localStorageOptions: When the `SQLiteStore` is passed to the `DataStack`'s `addStorage()` methods, tells the `DataStack` how to setup the persistent store. Defaults to `.None`.
|
||||
*/
|
||||
public init(fileURL: NSURL, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), localStorageOptions: LocalStorageOptions = nil) {
|
||||
public init(fileURL: URL, configuration: String? = nil, mappingModelBundles: [Bundle] = Bundle.allBundles, localStorageOptions: LocalStorageOptions = nil) {
|
||||
|
||||
self.fileURL = fileURL
|
||||
self.configuration = configuration
|
||||
@@ -60,10 +60,10 @@ public final class SQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
- parameter mappingModelBundles: a list of `NSBundle`s from which to search mapping models (*.xcmappingmodel) for migration
|
||||
- parameter localStorageOptions: When the `SQLiteStore` is passed to the `DataStack`'s `addStorage()` methods, tells the `DataStack` how to setup the persistent store. Defaults to `.None`.
|
||||
*/
|
||||
public init(fileName: String, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), localStorageOptions: LocalStorageOptions = nil) {
|
||||
public init(fileName: String, configuration: String? = nil, mappingModelBundles: [Bundle] = Bundle.allBundles, localStorageOptions: LocalStorageOptions = nil) {
|
||||
|
||||
self.fileURL = SQLiteStore.defaultRootDirectory
|
||||
.URLByAppendingPathComponent(fileName, isDirectory: false)
|
||||
self.fileURL = try! SQLiteStore.defaultRootDirectory
|
||||
.appendingPathComponent(fileName, isDirectory: false)
|
||||
self.configuration = configuration
|
||||
self.mappingModelBundles = mappingModelBundles
|
||||
self.localStorageOptions = localStorageOptions
|
||||
@@ -81,7 +81,7 @@ public final class SQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
|
||||
self.fileURL = SQLiteStore.defaultFileURL
|
||||
self.configuration = nil
|
||||
self.mappingModelBundles = NSBundle.allBundles()
|
||||
self.mappingModelBundles = Bundle.allBundles
|
||||
self.localStorageOptions = nil
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ public final class SQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
public func didAddToDataStack(dataStack: DataStack) {
|
||||
public func didAddToDataStack(_ dataStack: DataStack) {
|
||||
|
||||
self.dataStack = dataStack
|
||||
}
|
||||
@@ -117,7 +117,7 @@ public final class SQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
public func didRemoveFromDataStack(dataStack: DataStack) {
|
||||
public func didRemoveFromDataStack(_ dataStack: DataStack) {
|
||||
|
||||
self.dataStack = nil
|
||||
}
|
||||
@@ -128,12 +128,12 @@ public final class SQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
/**
|
||||
The `NSURL` that points to the SQLite file
|
||||
*/
|
||||
public let fileURL: NSURL
|
||||
public let fileURL: URL
|
||||
|
||||
/**
|
||||
The `NSBundle`s from which to search mapping models for migrations
|
||||
*/
|
||||
public let mappingModelBundles: [NSBundle]
|
||||
public let mappingModelBundles: [Bundle]
|
||||
|
||||
/**
|
||||
Options that tell the `DataStack` how to setup the persistent store
|
||||
@@ -143,15 +143,15 @@ public final class SQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
/**
|
||||
The options dictionary for the specified `LocalStorageOptions`
|
||||
*/
|
||||
public func storeOptionsForOptions(options: LocalStorageOptions) -> [String: AnyObject]? {
|
||||
public func storeOptionsForOptions(_ options: LocalStorageOptions) -> [String: AnyObject]? {
|
||||
|
||||
if options == .None {
|
||||
if options == .none {
|
||||
|
||||
return self.storeOptions
|
||||
}
|
||||
|
||||
var storeOptions = self.storeOptions ?? [:]
|
||||
if options.contains(.AllowSynchronousLightweightMigration) {
|
||||
if options.contains(.allowSynchronousLightweightMigration) {
|
||||
|
||||
storeOptions[NSMigratePersistentStoresAutomaticallyOption] = true
|
||||
storeOptions[NSInferMappingModelAutomaticallyOption] = true
|
||||
@@ -162,7 +162,7 @@ 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: NSManagedObjectModel) throws {
|
||||
|
||||
// TODO: check if attached to persistent store
|
||||
|
||||
@@ -170,44 +170,43 @@ public final class SQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
try cs_autoreleasepool {
|
||||
|
||||
let journalUpdatingCoordinator = NSPersistentStoreCoordinator(managedObjectModel: soureModel)
|
||||
let store = try journalUpdatingCoordinator.addPersistentStoreWithType(
|
||||
self.dynamicType.storeType,
|
||||
configuration: self.configuration,
|
||||
URL: fileURL,
|
||||
let store = try journalUpdatingCoordinator.addPersistentStore(
|
||||
ofType: self.dynamicType.storeType,
|
||||
configurationName: self.configuration,
|
||||
at: fileURL,
|
||||
options: [NSSQLitePragmasOption: ["journal_mode": "DELETE"]]
|
||||
)
|
||||
try journalUpdatingCoordinator.removePersistentStore(store)
|
||||
try NSFileManager.defaultManager().removeItemAtURL(fileURL)
|
||||
try journalUpdatingCoordinator.remove(store)
|
||||
try FileManager.default.removeItem(at: fileURL)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal static let defaultRootDirectory: NSURL = {
|
||||
internal static let defaultRootDirectory: URL = {
|
||||
|
||||
#if os(tvOS)
|
||||
let systemDirectorySearchPath = NSSearchPathDirectory.CachesDirectory
|
||||
#else
|
||||
let systemDirectorySearchPath = NSSearchPathDirectory.ApplicationSupportDirectory
|
||||
let systemDirectorySearchPath = FileManager.SearchPathDirectory.applicationSupportDirectory
|
||||
#endif
|
||||
|
||||
let defaultSystemDirectory = NSFileManager
|
||||
.defaultManager()
|
||||
.URLsForDirectory(systemDirectorySearchPath, inDomains: .UserDomainMask).first!
|
||||
let defaultSystemDirectory = FileManager.default
|
||||
.urlsForDirectory(systemDirectorySearchPath, inDomains: .userDomainMask).first!
|
||||
|
||||
return defaultSystemDirectory.URLByAppendingPathComponent(
|
||||
NSBundle.mainBundle().bundleIdentifier ?? "com.CoreStore.DataStack",
|
||||
return try! defaultSystemDirectory.appendingPathComponent(
|
||||
Bundle.main.bundleIdentifier ?? "com.CoreStore.DataStack",
|
||||
isDirectory: true
|
||||
)
|
||||
}()
|
||||
|
||||
internal static let defaultFileURL = SQLiteStore.defaultRootDirectory
|
||||
.URLByAppendingPathComponent(
|
||||
(NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as? String) ?? "CoreData",
|
||||
internal static let defaultFileURL = try! SQLiteStore.defaultRootDirectory
|
||||
.appendingPathComponent(
|
||||
(Bundle.main.objectForInfoDictionaryKey("CFBundleName") as? String) ?? "CoreData",
|
||||
isDirectory: false
|
||||
)
|
||||
.URLByAppendingPathExtension("sqlite")
|
||||
.appendingPathExtension("sqlite")
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
@@ -54,12 +54,12 @@ public protocol StorageInterface: class {
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
func didAddToDataStack(dataStack: DataStack)
|
||||
func didAddToDataStack(_ dataStack: DataStack)
|
||||
|
||||
/**
|
||||
Do not call directly. Used by the `DataStack` internally.
|
||||
*/
|
||||
func didRemoveFromDataStack(dataStack: DataStack)
|
||||
func didRemoveFromDataStack(_ dataStack: DataStack)
|
||||
}
|
||||
|
||||
|
||||
@@ -82,27 +82,27 @@ public protocol DefaultInitializableStore: StorageInterface {
|
||||
/**
|
||||
The `LocalStorageOptions` provides settings that tells the `DataStack` how to setup the persistent store for `LocalStorage` implementers.
|
||||
*/
|
||||
public struct LocalStorageOptions: OptionSetType, NilLiteralConvertible {
|
||||
public struct LocalStorageOptions: OptionSet, NilLiteralConvertible {
|
||||
|
||||
/**
|
||||
Tells the `DataStack` that the store should not be migrated or recreated, and should simply fail on model mismatch
|
||||
*/
|
||||
public static let None = LocalStorageOptions(rawValue: 0)
|
||||
public static let none = LocalStorageOptions(rawValue: 0)
|
||||
|
||||
/**
|
||||
Tells the `DataStack` to delete and recreate the store on model mismatch, otherwise exceptions will be thrown on failure instead
|
||||
*/
|
||||
public static let RecreateStoreOnModelMismatch = LocalStorageOptions(rawValue: 1 << 0)
|
||||
public static let recreateStoreOnModelMismatch = LocalStorageOptions(rawValue: 1 << 0)
|
||||
|
||||
/**
|
||||
Tells the `DataStack` to prevent progressive migrations for the store
|
||||
*/
|
||||
public static let PreventProgressiveMigration = LocalStorageOptions(rawValue: 1 << 1)
|
||||
public static let preventProgressiveMigration = LocalStorageOptions(rawValue: 1 << 1)
|
||||
|
||||
/**
|
||||
Tells the `DataStack` to allow lightweight migration for the store when added synchronously
|
||||
*/
|
||||
public static let AllowSynchronousLightweightMigration = LocalStorageOptions(rawValue: 1 << 2)
|
||||
public static let allowSynchronousLightweightMigration = LocalStorageOptions(rawValue: 1 << 2)
|
||||
|
||||
|
||||
|
||||
@@ -138,12 +138,12 @@ public protocol LocalStorage: StorageInterface {
|
||||
/**
|
||||
The `NSURL` that points to the store file
|
||||
*/
|
||||
var fileURL: NSURL { get }
|
||||
var fileURL: URL { get }
|
||||
|
||||
/**
|
||||
The `NSBundle`s from which to search mapping models (*.xcmappingmodel) for migrations
|
||||
*/
|
||||
var mappingModelBundles: [NSBundle] { get }
|
||||
var mappingModelBundles: [Bundle] { get }
|
||||
|
||||
/**
|
||||
Options that tell the `DataStack` how to setup the persistent store
|
||||
@@ -153,21 +153,21 @@ public protocol LocalStorage: StorageInterface {
|
||||
/**
|
||||
The options dictionary for the specified `LocalStorageOptions`
|
||||
*/
|
||||
func storeOptionsForOptions(options: LocalStorageOptions) -> [String: AnyObject]?
|
||||
func storeOptionsForOptions(_ options: LocalStorageOptions) -> [String: AnyObject]?
|
||||
|
||||
/**
|
||||
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: NSManagedObjectModel) throws
|
||||
}
|
||||
|
||||
internal extension LocalStorage {
|
||||
|
||||
internal func matchesPersistentStore(persistentStore: NSPersistentStore) -> Bool {
|
||||
internal func matchesPersistentStore(_ persistentStore: NSPersistentStore) -> Bool {
|
||||
|
||||
return persistentStore.type == self.dynamicType.storeType
|
||||
&& persistentStore.configurationName == (self.configuration ?? Into.defaultConfigurationName)
|
||||
&& persistentStore.URL == self.fileURL
|
||||
&& persistentStore.url == self.fileURL
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,22 +177,22 @@ internal extension LocalStorage {
|
||||
/**
|
||||
The `CloudStorageOptions` provides settings that tells the `DataStack` how to setup the persistent store for `LocalStorage` implementers.
|
||||
*/
|
||||
public struct CloudStorageOptions: OptionSetType, NilLiteralConvertible {
|
||||
public struct CloudStorageOptions: OptionSet, NilLiteralConvertible {
|
||||
|
||||
/**
|
||||
Tells the `DataStack` that the store should not be migrated or recreated, and should simply fail on model mismatch
|
||||
*/
|
||||
public static let None = CloudStorageOptions(rawValue: 0)
|
||||
public static let none = CloudStorageOptions(rawValue: 0)
|
||||
|
||||
/**
|
||||
Tells the `DataStack` to delete and recreate the local store from the cloud store on model mismatch, otherwise exceptions will be thrown on failure instead
|
||||
*/
|
||||
public static let RecreateLocalStoreOnModelMismatch = CloudStorageOptions(rawValue: 1 << 0)
|
||||
public static let recreateLocalStoreOnModelMismatch = CloudStorageOptions(rawValue: 1 << 0)
|
||||
|
||||
/**
|
||||
Tells the `DataStack` to allow lightweight migration for the store when added synchronously
|
||||
*/
|
||||
public static let AllowSynchronousLightweightMigration = CloudStorageOptions(rawValue: 1 << 2)
|
||||
public static let allowSynchronousLightweightMigration = CloudStorageOptions(rawValue: 1 << 2)
|
||||
|
||||
|
||||
// MARK: OptionSetType
|
||||
@@ -227,7 +227,7 @@ public protocol CloudStorage: StorageInterface {
|
||||
/**
|
||||
The `NSURL` that points to the store file
|
||||
*/
|
||||
var cacheFileURL: NSURL { get }
|
||||
var cacheFileURL: URL { get }
|
||||
|
||||
/**
|
||||
Options that tell the `DataStack` how to setup the persistent store
|
||||
@@ -237,24 +237,24 @@ public protocol CloudStorage: StorageInterface {
|
||||
/**
|
||||
The options dictionary for the specified `CloudStorageOptions`
|
||||
*/
|
||||
func storeOptionsForOptions(options: CloudStorageOptions) -> [String: AnyObject]?
|
||||
func storeOptionsForOptions(_ options: CloudStorageOptions) -> [String: AnyObject]?
|
||||
|
||||
/**
|
||||
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: NSManagedObjectModel) throws
|
||||
}
|
||||
|
||||
internal extension CloudStorage {
|
||||
|
||||
internal func matchesPersistentStore(persistentStore: NSPersistentStore) -> Bool {
|
||||
internal func matchesPersistentStore(_ persistentStore: NSPersistentStore) -> Bool {
|
||||
|
||||
guard persistentStore.type == self.dynamicType.storeType
|
||||
&& persistentStore.configurationName == (self.configuration ?? Into.defaultConfigurationName) else {
|
||||
|
||||
return false
|
||||
}
|
||||
guard persistentStore.URL == self.cacheFileURL else {
|
||||
guard persistentStore.url == self.cacheFileURL else {
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user