mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-04-30 12:34:16 +02:00
WIP: objective C interface
This commit is contained in:
@@ -31,14 +31,14 @@ import CoreData
|
||||
/**
|
||||
A storage interface that is backed only by memory.
|
||||
*/
|
||||
public class InMemoryStore: StorageInterface, DefaultInitializableStore {
|
||||
public final class InMemoryStore: StorageInterface, DefaultInitializableStore {
|
||||
|
||||
/**
|
||||
Initializes an `InMemoryStore` for the specified configuration
|
||||
|
||||
- parameter configuration: an optional configuration name from the model file. If not specified, defaults to `nil`, the "Default" configuration.
|
||||
*/
|
||||
public required init(configuration: String?) {
|
||||
public init(configuration: String?) {
|
||||
|
||||
self.configuration = configuration
|
||||
}
|
||||
@@ -49,7 +49,7 @@ public class InMemoryStore: StorageInterface, DefaultInitializableStore {
|
||||
/**
|
||||
Initializes an `InMemoryStore` with the "Default" configuration
|
||||
*/
|
||||
public required init() {
|
||||
public init() {
|
||||
|
||||
self.configuration = nil
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ import Foundation
|
||||
A storage interface backed by an SQLite database that was created before CoreStore 2.0.0.
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was depending on CoreStore's default directories prior to 2.0.0, make sure to use `LegacySQLiteStore` instead of `SQLiteStore`.
|
||||
*/
|
||||
public final class LegacySQLiteStore: SQLiteStore {
|
||||
public final class LegacySQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
|
||||
/**
|
||||
Initializes an SQLite store interface from the given SQLite file URL. When this instance is passed to the `DataStack`'s `addStorage()` methods, a new SQLite file will be created if it does not exist.
|
||||
@@ -42,14 +42,12 @@ public final class LegacySQLiteStore: SQLiteStore {
|
||||
- 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 required init(fileURL: NSURL, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), localStorageOptions: LocalStorageOptions = nil) {
|
||||
public init(fileURL: NSURL, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), localStorageOptions: LocalStorageOptions = nil) {
|
||||
|
||||
super.init(
|
||||
fileURL: fileURL,
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles,
|
||||
localStorageOptions: localStorageOptions
|
||||
)
|
||||
self.fileURL = fileURL
|
||||
self.configuration = configuration
|
||||
self.mappingModelBundles = mappingModelBundles
|
||||
self.localStorageOptions = localStorageOptions
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,23 +59,97 @@ public final class LegacySQLiteStore: SQLiteStore {
|
||||
- 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 required init(fileName: String, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), localStorageOptions: LocalStorageOptions = nil) {
|
||||
public init(fileName: String, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), localStorageOptions: LocalStorageOptions = nil) {
|
||||
|
||||
super.init(
|
||||
fileURL: LegacySQLiteStore.defaultRootDirectory.URLByAppendingPathComponent(
|
||||
fileName,
|
||||
isDirectory: false
|
||||
),
|
||||
configuration: configuration,
|
||||
mappingModelBundles: mappingModelBundles,
|
||||
localStorageOptions: localStorageOptions
|
||||
self.fileURL = LegacySQLiteStore.defaultRootDirectory.URLByAppendingPathComponent(
|
||||
fileName,
|
||||
isDirectory: false
|
||||
)
|
||||
self.configuration = configuration
|
||||
self.mappingModelBundles = mappingModelBundles
|
||||
self.localStorageOptions = localStorageOptions
|
||||
}
|
||||
|
||||
|
||||
// MARK: SQLiteStore
|
||||
// MARK: DefaultInitializableStore
|
||||
|
||||
internal override class var defaultRootDirectory: NSURL {
|
||||
/**
|
||||
Initializes an `LegacySQLiteStore` with an all-default settings: a `fileURL` pointing to a "<Application name>.sqlite" file in the "Application Support" directory (or the "Caches" directory on tvOS), a `nil` `configuration` pertaining to the "Default" configuration, a `mappingModelBundles` set to search all `NSBundle`s, and `localStorageOptions` set to `.AllowProgresiveMigration`.
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was depending on CoreStore's default directories prior to 2.0.0, make sure to use `LegacySQLiteStore` instead of `SQLiteStore`.
|
||||
*/
|
||||
public init() {
|
||||
|
||||
self.fileURL = LegacySQLiteStore.defaultFileURL
|
||||
self.configuration = nil
|
||||
self.mappingModelBundles = NSBundle.allBundles()
|
||||
self.localStorageOptions = nil
|
||||
}
|
||||
|
||||
|
||||
// MAKR: LocalStorage
|
||||
|
||||
/**
|
||||
The `NSURL` that points to the SQLite file
|
||||
*/
|
||||
public let fileURL: NSURL
|
||||
|
||||
/**
|
||||
The `NSBundle`s from which to search mapping models for migrations
|
||||
*/
|
||||
public let mappingModelBundles: [NSBundle]
|
||||
|
||||
/**
|
||||
Options that tell the `DataStack` how to setup the persistent store
|
||||
*/
|
||||
public var localStorageOptions: LocalStorageOptions
|
||||
|
||||
|
||||
// MARK: StorageInterface
|
||||
|
||||
/**
|
||||
The string identifier for the `NSPersistentStore`'s `type` property. For `SQLiteStore`s, this is always set to `NSSQLiteStoreType`.
|
||||
*/
|
||||
public static let storeType = NSSQLiteStoreType
|
||||
|
||||
/**
|
||||
The configuration name in the model file
|
||||
*/
|
||||
public let configuration: String?
|
||||
|
||||
/**
|
||||
The options dictionary for the `NSPersistentStore`. For `SQLiteStore`s, this is always set to
|
||||
```
|
||||
[NSSQLitePragmasOption: ["journal_mode": "WAL"]]
|
||||
```
|
||||
*/
|
||||
public let storeOptions: [String: AnyObject]? = [NSSQLitePragmasOption: ["journal_mode": "WAL"]]
|
||||
|
||||
/**
|
||||
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 {
|
||||
|
||||
// TODO: check if attached to persistent store
|
||||
|
||||
let fileURL = self.fileURL
|
||||
try autoreleasepool {
|
||||
|
||||
let journalUpdatingCoordinator = NSPersistentStoreCoordinator(managedObjectModel: soureModel)
|
||||
let store = try journalUpdatingCoordinator.addPersistentStoreWithType(
|
||||
self.dynamicType.storeType,
|
||||
configuration: self.configuration,
|
||||
URL: fileURL,
|
||||
options: [NSSQLitePragmasOption: ["journal_mode": "DELETE"]]
|
||||
)
|
||||
try journalUpdatingCoordinator.removePersistentStore(store)
|
||||
try NSFileManager.defaultManager().removeItemAtURL(fileURL)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal static let defaultRootDirectory: NSURL = {
|
||||
|
||||
#if os(tvOS)
|
||||
let systemDirectorySearchPath = NSSearchPathDirectory.CachesDirectory
|
||||
@@ -89,24 +161,9 @@ public final class LegacySQLiteStore: SQLiteStore {
|
||||
systemDirectorySearchPath,
|
||||
inDomains: .UserDomainMask
|
||||
).first!
|
||||
}
|
||||
}()
|
||||
|
||||
internal override class var defaultFileURL: NSURL {
|
||||
|
||||
return LegacySQLiteStore.defaultRootDirectory
|
||||
.URLByAppendingPathComponent(DataStack.applicationName, isDirectory: false)
|
||||
.URLByAppendingPathExtension("sqlite")
|
||||
}
|
||||
|
||||
|
||||
// MARK: DefaultInitializableStore
|
||||
|
||||
/**
|
||||
Initializes an `LegacySQLiteStore` with an all-default settings: a `fileURL` pointing to a "<Application name>.sqlite" file in the "Application Support" directory (or the "Caches" directory on tvOS), a `nil` `configuration` pertaining to the "Default" configuration, a `mappingModelBundles` set to search all `NSBundle`s, and `localStorageOptions` set to `.AllowProgresiveMigration`.
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was depending on CoreStore's default directories prior to 2.0.0, make sure to use `LegacySQLiteStore` instead of `SQLiteStore`.
|
||||
*/
|
||||
public required init() {
|
||||
|
||||
super.init(fileURL: LegacySQLiteStore.defaultFileURL)
|
||||
}
|
||||
internal static let defaultFileURL = LegacySQLiteStore.defaultRootDirectory
|
||||
.URLByAppendingPathComponent(DataStack.applicationName, isDirectory: false)
|
||||
.URLByAppendingPathExtension("sqlite")
|
||||
}
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
//
|
||||
// LocalStorageOptions.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: - LocalStorageOptions
|
||||
|
||||
/**
|
||||
The `LocalStorageOptions` provides settings that tells the `DataStack` how to setup the persistent store for `LocalStorage` implementers.
|
||||
*/
|
||||
public struct LocalStorageOptions: OptionSetType, 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)
|
||||
|
||||
/**
|
||||
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)
|
||||
|
||||
/**
|
||||
Tells the `DataStack` to prevent progressive migrations for the store
|
||||
*/
|
||||
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)
|
||||
|
||||
|
||||
// MARK: OptionSetType
|
||||
|
||||
public init(rawValue: Int) {
|
||||
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
|
||||
// MARK: RawRepresentable
|
||||
|
||||
public let rawValue: Int
|
||||
|
||||
|
||||
// MARK: NilLiteralConvertible
|
||||
|
||||
public init(nilLiteral: ()) {
|
||||
|
||||
self.rawValue = 0
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ import CoreData
|
||||
A storage interface that is backed by an SQLite database.
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was depending on CoreStore's default directories prior to 2.0.0, make sure to use `LegacySQLiteStore` instead of `SQLiteStore`.
|
||||
*/
|
||||
public class SQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
public final class SQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
|
||||
/**
|
||||
Initializes an SQLite store interface from the given SQLite file URL. When this instance is passed to the `DataStack`'s `addStorage()` methods, a new SQLite file will be created if it does not exist.
|
||||
@@ -42,7 +42,7 @@ public class SQLiteStore: 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 required init(fileURL: NSURL, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), localStorageOptions: LocalStorageOptions = nil) {
|
||||
public init(fileURL: NSURL, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), localStorageOptions: LocalStorageOptions = nil) {
|
||||
|
||||
self.fileURL = fileURL
|
||||
self.configuration = configuration
|
||||
@@ -59,7 +59,7 @@ public class SQLiteStore: 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 required init(fileName: String, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), localStorageOptions: LocalStorageOptions = nil) {
|
||||
public init(fileName: String, configuration: String? = nil, mappingModelBundles: [NSBundle] = NSBundle.allBundles(), localStorageOptions: LocalStorageOptions = nil) {
|
||||
|
||||
self.fileURL = SQLiteStore.defaultRootDirectory
|
||||
.URLByAppendingPathComponent(fileName, isDirectory: false)
|
||||
@@ -75,7 +75,7 @@ public class SQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
Initializes an `SQLiteStore` with an all-default settings: a `fileURL` pointing to a "<Application name>.sqlite" file in the "Application Support/<bundle id>" directory (or the "Caches/<bundle id>" directory on tvOS), a `nil` `configuration` pertaining to the "Default" configuration, a `mappingModelBundles` set to search all `NSBundle`s, and `localStorageOptions` set to `.AllowProgresiveMigration`.
|
||||
- Warning: The default SQLite file location for the `LegacySQLiteStore` and `SQLiteStore` are different. If the app was depending on CoreStore's default directories prior to 2.0.0, make sure to use `LegacySQLiteStore` instead of `SQLiteStore`.
|
||||
*/
|
||||
public required init() {
|
||||
public init() {
|
||||
|
||||
self.fileURL = SQLiteStore.defaultFileURL
|
||||
self.configuration = nil
|
||||
@@ -145,9 +145,9 @@ public class SQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
// MARK: Private
|
||||
|
||||
internal class var defaultRootDirectory: NSURL {
|
||||
internal static let defaultRootDirectory: NSURL = {
|
||||
|
||||
#if os(tvOS)
|
||||
let systemDirectorySearchPath = NSSearchPathDirectory.CachesDirectory
|
||||
@@ -163,14 +163,12 @@ public class SQLiteStore: LocalStorage, DefaultInitializableStore {
|
||||
NSBundle.mainBundle().bundleIdentifier ?? "com.CoreStore.DataStack",
|
||||
isDirectory: true
|
||||
)
|
||||
}
|
||||
}()
|
||||
|
||||
internal class var defaultFileURL: NSURL {
|
||||
|
||||
let applicationName = (NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as? String) ?? "CoreData"
|
||||
|
||||
return SQLiteStore.defaultRootDirectory
|
||||
.URLByAppendingPathComponent(applicationName, isDirectory: false)
|
||||
.URLByAppendingPathExtension("sqlite")
|
||||
}
|
||||
internal static let defaultFileURL = SQLiteStore.defaultRootDirectory
|
||||
.URLByAppendingPathComponent(
|
||||
(NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as? String) ?? "CoreData",
|
||||
isDirectory: false
|
||||
)
|
||||
.URLByAppendingPathExtension("sqlite")
|
||||
}
|
||||
|
||||
@@ -64,6 +64,56 @@ public protocol DefaultInitializableStore: StorageInterface {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - LocalStorageOptions
|
||||
|
||||
/**
|
||||
The `LocalStorageOptions` provides settings that tells the `DataStack` how to setup the persistent store for `LocalStorage` implementers.
|
||||
*/
|
||||
public struct LocalStorageOptions: OptionSetType, 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)
|
||||
|
||||
/**
|
||||
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)
|
||||
|
||||
/**
|
||||
Tells the `DataStack` to prevent progressive migrations for the store
|
||||
*/
|
||||
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)
|
||||
|
||||
|
||||
// MARK: OptionSetType
|
||||
|
||||
public init(rawValue: Int) {
|
||||
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
|
||||
// MARK: RawRepresentable
|
||||
|
||||
public let rawValue: Int
|
||||
|
||||
|
||||
// MARK: NilLiteralConvertible
|
||||
|
||||
public init(nilLiteral: ()) {
|
||||
|
||||
self.rawValue = 0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - LocalStorage
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user