WIP: objective C storages

This commit is contained in:
John Estropia
2016-03-18 20:32:23 +09:00
parent f2df8f7171
commit 1507ac63f9
7 changed files with 188 additions and 147 deletions

View File

@@ -23,7 +23,7 @@ public extension CSCoreStore {
Returns the entity name-to-class type mapping from the `defaultStack`'s model.
*/
@objc
public static var entityTypesByName: [String: NSManagedObject.Type] {
public static var entityClassesByName: [String: NSManagedObject.Type] {
return CoreStore.defaultStack.entityTypesByName
}
@@ -32,69 +32,72 @@ public extension CSCoreStore {
Returns the `NSEntityDescription` for the specified `NSManagedObject` subclass from `defaultStack`'s model.
*/
@objc
public static func entityDescriptionForType(type: NSManagedObject.Type) -> NSEntityDescription? {
public static func entityDescriptionForClass(type: NSManagedObject.Type) -> NSEntityDescription? {
return CoreStore.defaultStack.entityDescriptionForType(type)
}
/**
Creates an `CSSQLiteStore` with default parameters and adds it to the `defaultStack`. This method blocks until completion.
Creates an `CSInMemoryStore` with default parameters and adds it to the `defaultStack`. This method blocks until completion.
```
CSSQLiteStore *storage = [CSCoreStore addStorageAndWaitAndReturnError:&error];
CSSQLiteStore *storage = [CSCoreStore addInMemoryStorageAndWaitAndReturnError:&error];
```
- returns: the local SQLite storage added to the `defaultStack`
- returns: the `CSInMemoryStore` added to the `defaultStack`
*/
@objc
public static func addStorageAndWait() throws -> CSSQLiteStore {
public static func addInMemoryStorageAndWait() throws -> CSInMemoryStore {
return try CoreStore.defaultStack.addStorageAndWait(InMemoryStore).objc
}
/**
Creates an `CSSQLiteStore` with default parameters and adds it to the `defaultStack`. This method blocks until completion.
```
CSSQLiteStore *storage = [CSCoreStore addSQLiteStorageAndWaitAndReturnError:&error];
```
- returns: the `CSSQLiteStore` added to the `defaultStack`
*/
@objc
public static func addSQLiteStorageAndWait() throws -> CSSQLiteStore {
return try CoreStore.defaultStack.addStorageAndWait(SQLiteStore).objc
}
/**
Adds a `StorageInterface` to the `defaultStack` and blocks until completion.
Adds a `CSInMemoryStore` to the `defaultStack` and blocks until completion.
```
CSInMemoryStore *storage = [CoreStore
addStorageAndWait: [[InMemoryStore alloc] initWithConfiguration: @"Config1"]
NSError *error;
CSInMemoryStore *storage = [CSCoreStore
addStorageAndWait: [[CSInMemoryStore alloc] initWithConfiguration: @"Config1"]
error: &error];
```
- parameter storage: the `StorageInterface`
- returns: the `StorageInterface` added to the `defaultStack`
- parameter storage: the `CSInMemoryStore`
- returns: the `CSInMemoryStore` added to the `defaultStack`
*/
@objc
public static func addStorageAndWait(storage: CSInMemoryStore) throws -> CSInMemoryStore {
public static func addInMemoryStorageAndWait(storage: CSInMemoryStore) throws -> CSInMemoryStore {
return try CoreStore.defaultStack.addStorageAndWait(storage.swift).objc
}
/**
Creates a `LocalStorageface` of the specified store type with default values and adds it to the `defaultStack`. This method blocks until completion.
Adds a `CSSQLiteStore` to the `defaultStack` and blocks until completion.
```
try CoreStore.addStorageAndWait(SQLiteStore)
NSError *error;
CSSQLiteStore *storage = [CSCoreStore
addStorageAndWait: [[CSSQLiteStore alloc] initWithConfiguration: @"Config1"]
error: &error];
```
- parameter storeType: the `LocalStorageface` type
- returns: the local storage added to the `defaultStack`
- parameter storage: the `CSSQLiteStore`
- returns: the `CSSQLiteStore` added to the `defaultStack`
*/
// @objc
// public static func addStorageAndWait<T: LocalStorage where T: DefaultInitializableStore>(storageType: T.Type) throws -> T {
//
// return try self.defaultStack.swift.addStorageAndWait(storageType.init())
// }
/**
Adds a `LocalStorage` to the `defaultStack` and blocks until completion.
```
try CoreStore.addStorageAndWait(SQLiteStore(configuration: "Config1"))
```
- parameter storage: the local storage
- 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.
*/
// @objc
// public static func addStorageAndWait<T: LocalStorage>(storage: T) throws -> T {
//
// return try self.defaultStack.swift.addStorageAndWait(storage)
// }
@objc
public static func addSQLiteStorageAndWait(storage: CSSQLiteStore) throws -> CSSQLiteStore {
return try CoreStore.defaultStack.addStorageAndWait(storage.swift).objc
}
}

View File

@@ -126,6 +126,97 @@ public final class CSDataStack: NSObject, CoreStoreBridge {
)
}
/**
Returns the stack's model version. The version string is the same as the name of the version-specific .xcdatamodeld file.
*/
@objc
public var modelVersion: String {
return self.swift.modelVersion
}
/**
Returns the entity name-to-class type mapping from the stack's model.
*/
@objc
public var entityClassesByName: [String: NSManagedObject.Type] {
return self.swift.entityTypesByName
}
/**
Returns the `NSEntityDescription` for the specified `NSManagedObject` subclass from stack's model.
*/
@objc
public func entityDescriptionForClass(type: NSManagedObject.Type) -> NSEntityDescription? {
return self.swift.entityDescriptionForType(type)
}
/**
Creates an `CSInMemoryStore` with default parameters and adds it to the stack. This method blocks until completion.
```
CSSQLiteStore *storage = [dataStack addInMemoryStorageAndWaitAndReturnError:&error];
```
- returns: the `CSInMemoryStore` added to the stack
*/
@objc
public func addInMemoryStorageAndWait() throws -> CSInMemoryStore {
return try self.swift.addStorageAndWait(InMemoryStore).objc
}
/**
Creates an `CSSQLiteStore` with default parameters and adds it to the stack. This method blocks until completion.
```
CSSQLiteStore *storage = [dataStack addSQLiteStorageAndWaitAndReturnError:&error];
```
- returns: the `CSSQLiteStore` added to the stack
*/
@objc
public func addSQLiteStorageAndWait() throws -> CSSQLiteStore {
return try self.swift.addStorageAndWait(SQLiteStore).objc
}
/**
Adds a `CSInMemoryStore` to the stack and blocks until completion.
```
NSError *error;
CSInMemoryStore *storage = [dataStack
addStorageAndWait: [[CSInMemoryStore alloc] initWithConfiguration: @"Config1"]
error: &error];
```
- parameter storage: the `CSInMemoryStore`
- returns: the `CSInMemoryStore` added to the stack
*/
@objc
public func addInMemoryStorageAndWait(storage: CSInMemoryStore) throws -> CSInMemoryStore {
return try self.swift.addStorageAndWait(storage.swift).objc
}
/**
Adds a `CSSQLiteStore` to the stack and blocks until completion.
```
NSError *error;
CSSQLiteStore *storage = [dataStack
addStorageAndWait: [[CSSQLiteStore alloc] initWithConfiguration: @"Config1"]
error: &error];
```
- parameter storage: the `CSSQLiteStore`
- returns: the `CSSQLiteStore` added to the stack
*/
@objc
public func addSQLiteStorageAndWait(storage: CSSQLiteStore) throws -> CSSQLiteStore {
return try self.swift.addStorageAndWait(storage.swift).objc
}
// MARK: NSObject

View File

@@ -1,75 +0,0 @@
//
// CSLegacySQLiteStore.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: - LegacySQLiteStore
extension LegacySQLiteStore: CoreStoreBridgeable {
// MARK: CoreStoreBridgeable
public typealias ObjCType = CSLegacySQLiteStore
}
// MARK: - CSSQLiteStore
/**
The `CSLegacySQLiteStore` serves as the Objective-C bridging type for `LegacySQLiteStore`.
*/
@objc
public final class CSLegacySQLiteStore: NSObject, CoreStoreBridge {
// MARK: NSObject
public override var hash: Int {
return ObjectIdentifier(self.swift).hashValue
}
public override func isEqual(object: AnyObject?) -> Bool {
guard let object = object as? CSLegacySQLiteStore else {
return false
}
return self.swift === object.swift
}
// MARK: CoreStoreBridge
public let swift: LegacySQLiteStore
public required init(_ swiftObject: LegacySQLiteStore) {
self.swift = swiftObject
}
}

View File

@@ -51,17 +51,17 @@ public final class CSSQLiteStore: NSObject, CoreStoreBridge {
- parameter fileURL: the local file URL for the target SQLite persistent store. Note that if you have multiple configurations, you will need to specify a different `fileURL` explicitly for each of them.
- parameter configuration: an optional configuration name from the model file. If not specified, defaults to `nil`, the "Default" configuration. Note that if you have multiple configurations, you will need to specify a different `fileURL` explicitly for each of them.
- parameter mappingModelBundles: a list of `NSBundle`s from which to search mapping models for migration.
- parameter localStorageOptions: When the `CSSQLiteStore` is passed to the `CSDataStack`'s `addStorage()` methods, tells the `CSDataStack` how to setup the persistent store. Defaults to `.None`.
- parameter localStorageOptions: When the `CSSQLiteStore` is passed to the `CSDataStack`'s `addStorage()` methods, tells the `CSDataStack` how to setup the persistent store. Defaults to `[CSLocalStorageOptions none]`.
*/
@objc
public convenience init(fileURL: NSURL, configuration: String?, mappingModelBundles: [NSBundle]?, localStorageOptions: CSLocalStorageOptions) {
public convenience init(fileURL: NSURL, configuration: String?, mappingModelBundles: [NSBundle]?, localStorageOptions: Int) {
self.init(
SQLiteStore(
fileURL: fileURL,
configuration: configuration,
mappingModelBundles: mappingModelBundles ?? NSBundle.allBundles(),
localStorageOptions: LocalStorageOptions(rawValue: localStorageOptions.rawValue)
localStorageOptions: LocalStorageOptions(rawValue: localStorageOptions)
)
)
}
@@ -73,17 +73,17 @@ public final class CSSQLiteStore: NSObject, CoreStoreBridge {
- parameter fileName: the local filename for the SQLite persistent store in the "Application Support/<bundle id>" directory (or the "Caches/<bundle id>" directory on tvOS). Note that if you have multiple configurations, you will need to specify a different `fileName` explicitly for each of them.
- parameter configuration: an optional configuration name from the model file. If not specified, defaults to `nil`, the "Default" configuration. Note that if you have multiple configurations, you will need to specify a different `fileName` explicitly for each of them.
- parameter mappingModelBundles: a list of `NSBundle`s from which to search mapping models for migration
- parameter localStorageOptions: When the `CSSQLiteStore` is passed to the `CSDataStack`'s `addStorage()` methods, tells the `CSDataStack` how to setup the persistent store. Defaults to `.None`.
- parameter localStorageOptions: When the `CSSQLiteStore` is passed to the `CSDataStack`'s `addStorage()` methods, tells the `CSDataStack` how to setup the persistent store. Defaults to `[CSLocalStorageOptions none]`.
*/
@objc
public convenience init(fileName: String, configuration: String?, mappingModelBundles: [NSBundle]?, localStorageOptions: CSLocalStorageOptions) {
public convenience init(fileName: String, configuration: String?, mappingModelBundles: [NSBundle]?, localStorageOptions: Int) {
self.init(
SQLiteStore(
fileName: fileName,
configuration: configuration,
mappingModelBundles: mappingModelBundles ?? NSBundle.allBundles(),
localStorageOptions: LocalStorageOptions(rawValue: localStorageOptions.rawValue)
localStorageOptions: LocalStorageOptions(rawValue: localStorageOptions)
)
)
}
@@ -123,10 +123,9 @@ public final class CSSQLiteStore: NSObject, CoreStoreBridge {
Options that tell the `CSDataStack` how to setup the persistent store
*/
@objc
public var localStorageOptions: CSLocalStorageOptions {
public var localStorageOptions: Int {
// TODO: allow options
return CSLocalStorageOptions(rawValue: self.swift.localStorageOptions.rawValue) ?? .None
return self.swift.localStorageOptions.rawValue
}

View File

@@ -40,27 +40,39 @@ public protocol CSStorageInterface {
The `CSLocalStorageOptions` provides settings that tells the `CSDataStack` how to setup the persistent store for `CSLocalStorage` implementers.
*/
@objc
public enum CSLocalStorageOptions: Int {
public final class CSLocalStorageOptions: NSObject {
/**
Tells the `DataStack` that the store should not be migrated or recreated, and should simply fail on model mismatch
*/
case None = 0
@objc
public static let none = 0
/**
Tells the `DataStack` to delete and recreate the store on model mismatch, otherwise exceptions will be thrown on failure instead
*/
case RecreateStoreOnModelMismatch = 1
@objc
public static let recreateStoreOnModelMismatch = 1
/**
Tells the `DataStack` to prevent progressive migrations for the store
*/
case PreventProgressiveMigration = 2
@objc
public static let preventProgressiveMigration = 2
/**
Tells the `DataStack` to allow lightweight migration for the store when added synchronously
*/
case AllowSynchronousLightweightMigration = 4
@objc
public static let allowSynchronousLightweightMigration = 4
// MARK: Private
private override init() {
fatalError()
}
}
@@ -88,7 +100,7 @@ public protocol CSLocalStorage: CSStorageInterface {
Options that tell the `DataStack` how to setup the persistent store
*/
@objc
var localStorageOptions: CSLocalStorageOptions { get }
var localStorageOptions: Int { get }
/**
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)