mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-24 10:21:17 +01:00
WIP: prototype for ManagedObjectProtocol
This commit is contained in:
@@ -83,12 +83,12 @@ public final class AsynchronousDataTransaction: BaseDataTransaction {
|
||||
// MARK: BaseDataTransaction
|
||||
|
||||
/**
|
||||
Creates a new `NSManagedObject` with the specified entity type.
|
||||
Creates a new `NSManagedObject` or `ManagedObject` with the specified entity type.
|
||||
|
||||
- parameter into: the `Into` clause indicating the destination `NSManagedObject` entity type and the destination configuration
|
||||
- returns: a new `NSManagedObject` instance of the specified entity type.
|
||||
- parameter into: the `Into` clause indicating the destination `NSManagedObject` or `ManagedObject` entity type and the destination configuration
|
||||
- returns: a new `NSManagedObject` or `ManagedObject` instance of the specified entity type.
|
||||
*/
|
||||
public override func create<T: NSManagedObject>(_ into: Into<T>) -> T {
|
||||
public override func create<T: ManagedObjectProtocol>(_ into: Into<T>) -> T {
|
||||
|
||||
CoreStore.assert(
|
||||
!self.isCommitted,
|
||||
|
||||
@@ -45,32 +45,36 @@ public /*abstract*/ class BaseDataTransaction {
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a new `NSManagedObject` with the specified entity type.
|
||||
Creates a new `NSManagedObject` or `ManagedObject` with the specified entity type.
|
||||
|
||||
- parameter into: the `Into` clause indicating the destination `NSManagedObject` entity type and the destination configuration
|
||||
- returns: a new `NSManagedObject` instance of the specified entity type.
|
||||
- parameter into: the `Into` clause indicating the destination `NSManagedObject` or `ManagedObject` entity type and the destination configuration
|
||||
- returns: a new `NSManagedObject` or `ManagedObject` instance of the specified entity type.
|
||||
*/
|
||||
public func create<T: NSManagedObject>(_ into: Into<T>) -> T {
|
||||
public func create<T: ManagedObjectProtocol>(_ into: Into<T>) -> T {
|
||||
|
||||
let entityClass = (into.entityClass as! T.Type)
|
||||
let entityClass = into.entityClass
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to create an entity of type \(cs_typeName(entityClass)) outside its designated queue."
|
||||
)
|
||||
|
||||
let context = self.context
|
||||
let dataStack = context.parentStack!
|
||||
let entityIdentifier = EntityIdentifier(entityClass)
|
||||
if into.inferStoreIfPossible {
|
||||
|
||||
switch context.parentStack!.persistentStoreForEntityClass(
|
||||
entityClass,
|
||||
switch dataStack.persistentStore(
|
||||
for: entityIdentifier,
|
||||
configuration: nil,
|
||||
inferStoreIfPossible: true
|
||||
) {
|
||||
|
||||
case (let persistentStore?, _):
|
||||
let object = entityClass.createInContext(context)
|
||||
context.assign(object, to: persistentStore)
|
||||
return object
|
||||
return entityClass.cs_forceCreate(
|
||||
entityDescription: dataStack.entityDescription(for: entityIdentifier)!,
|
||||
into: context,
|
||||
assignTo: persistentStore
|
||||
)
|
||||
|
||||
case (nil, true):
|
||||
CoreStore.abort("Attempted to create an entity of type \(cs_typeName(entityClass)) with ambiguous destination persistent store, but the configuration name was not specified.")
|
||||
@@ -81,17 +85,19 @@ public /*abstract*/ class BaseDataTransaction {
|
||||
}
|
||||
else {
|
||||
|
||||
switch context.parentStack!.persistentStoreForEntityClass(
|
||||
entityClass,
|
||||
switch dataStack.persistentStore(
|
||||
for: entityIdentifier,
|
||||
configuration: into.configuration
|
||||
?? type(of: into).defaultConfigurationName,
|
||||
?? DataStack.defaultConfigurationName,
|
||||
inferStoreIfPossible: false
|
||||
) {
|
||||
|
||||
case (let persistentStore?, _):
|
||||
let object = entityClass.createInContext(context)
|
||||
context.assign(object, to: persistentStore)
|
||||
return object
|
||||
return entityClass.cs_forceCreate(
|
||||
entityDescription: dataStack.entityDescription(for: entityIdentifier)!,
|
||||
into: context,
|
||||
assignTo: persistentStore
|
||||
)
|
||||
|
||||
case (nil, true):
|
||||
CoreStore.abort("Attempted to create an entity of type \(cs_typeName(entityClass)) with ambiguous destination persistent store, but the configuration name was not specified.")
|
||||
@@ -143,7 +149,7 @@ public /*abstract*/ class BaseDataTransaction {
|
||||
)
|
||||
CoreStore.assert(
|
||||
into.inferStoreIfPossible
|
||||
|| (into.configuration ?? Into.defaultConfigurationName) == objectID.persistentStore?.configurationName,
|
||||
|| (into.configuration ?? DataStack.defaultConfigurationName) == objectID.persistentStore?.configurationName,
|
||||
"Attempted to update an entity of type \(cs_typeName(into.entityClass)) but the specified persistent store do not match the `NSManagedObjectID`."
|
||||
)
|
||||
return self.fetchExisting(objectID) as? T
|
||||
@@ -160,11 +166,10 @@ public /*abstract*/ class BaseDataTransaction {
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to delete an entity outside its designated queue."
|
||||
)
|
||||
guard let object = object else {
|
||||
|
||||
return
|
||||
}
|
||||
self.context.fetchExisting(object)?.deleteFromContext()
|
||||
let context = self.context
|
||||
object
|
||||
.flatMap(context.fetchExisting)
|
||||
.flatMap(context.delete)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -192,7 +197,7 @@ public /*abstract*/ class BaseDataTransaction {
|
||||
)
|
||||
|
||||
let context = self.context
|
||||
objects.forEach { context.fetchExisting($0)?.deleteFromContext() }
|
||||
objects.forEach { context.fetchExisting($0).flatMap(context.delete) }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -39,18 +39,18 @@ import CoreData
|
||||
let person = transaction.create(Into<MyPersonEntity>("Configuration1"))
|
||||
```
|
||||
*/
|
||||
public struct Into<T: NSManagedObject>: Hashable {
|
||||
public struct Into<T: ManagedObjectProtocol>: Hashable {
|
||||
|
||||
/**
|
||||
The associated `NSManagedObject` entity class
|
||||
The associated `NSManagedObject` or `ManagedObject` entity class
|
||||
*/
|
||||
public let entityClass: AnyClass
|
||||
public let entityClass: T.Type
|
||||
|
||||
/**
|
||||
The `NSPersistentStore` configuration name to associate objects from.
|
||||
May contain a `String` to pertain to a named configuration, or `nil` to pertain to the default configuration
|
||||
*/
|
||||
public let configuration: String?
|
||||
public let configuration: ModelConfiguration
|
||||
|
||||
/**
|
||||
Initializes an `Into` clause.
|
||||
@@ -58,7 +58,7 @@ public struct Into<T: NSManagedObject>: Hashable {
|
||||
let person = transaction.create(Into<MyPersonEntity>())
|
||||
```
|
||||
*/
|
||||
public init(){
|
||||
public init() {
|
||||
|
||||
self.init(entityClass: T.self, configuration: nil, inferStoreIfPossible: true)
|
||||
}
|
||||
@@ -75,22 +75,6 @@ public struct Into<T: NSManagedObject>: Hashable {
|
||||
self.init(entityClass: entity, configuration: nil, inferStoreIfPossible: true)
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes an `Into` clause with the specified entity class.
|
||||
```
|
||||
let person = transaction.create(Into(MyPersonEntity.self))
|
||||
```
|
||||
- parameter entityClass: the `NSManagedObject` class type to be created
|
||||
*/
|
||||
public init(_ entityClass: AnyClass) {
|
||||
|
||||
CoreStore.assert(
|
||||
entityClass is T.Type,
|
||||
"Attempted to create generic type \(cs_typeName(Into<T>.self)) with entity class \(cs_typeName(entityClass))"
|
||||
)
|
||||
self.init(entityClass: entityClass, configuration: nil, inferStoreIfPossible: true)
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes an `Into` clause with the specified configuration.
|
||||
```
|
||||
@@ -98,7 +82,7 @@ public struct Into<T: NSManagedObject>: Hashable {
|
||||
```
|
||||
- parameter configuration: the `NSPersistentStore` configuration name to associate the object to. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
|
||||
*/
|
||||
public init(_ configuration: String?) {
|
||||
public init(_ configuration: ModelConfiguration) {
|
||||
|
||||
self.init(entityClass: T.self, configuration: configuration, inferStoreIfPossible: false)
|
||||
}
|
||||
@@ -111,32 +95,15 @@ public struct Into<T: NSManagedObject>: Hashable {
|
||||
- parameter entity: the `NSManagedObject` type to be created
|
||||
- parameter configuration: the `NSPersistentStore` configuration name to associate the object to. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
|
||||
*/
|
||||
public init(_ entity: T.Type, _ configuration: String?) {
|
||||
public init(_ entity: T.Type, _ configuration: ModelConfiguration) {
|
||||
|
||||
self.init(entityClass: entity, configuration: configuration, inferStoreIfPossible: false)
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes an `Into` clause with the specified entity class and configuration.
|
||||
```
|
||||
let person = transaction.create(Into(MyPersonEntity.self, "Configuration1"))
|
||||
```
|
||||
- parameter entityClass: the `NSManagedObject` class type to be created
|
||||
- parameter configuration: the `NSPersistentStore` configuration name to associate the object to. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
|
||||
*/
|
||||
public init(_ entityClass: AnyClass, _ configuration: String?) {
|
||||
|
||||
CoreStore.assert(
|
||||
entityClass is T.Type,
|
||||
"Attempted to create generic type \(cs_typeName(Into<T>.self)) with entity class \(cs_typeName(entityClass))"
|
||||
)
|
||||
self.init(entityClass: entityClass, configuration: configuration, inferStoreIfPossible: false)
|
||||
}
|
||||
|
||||
|
||||
// MARK: Equatable
|
||||
|
||||
public static func == <T: NSManagedObject, U: NSManagedObject>(lhs: Into<T>, rhs: Into<U>) -> Bool {
|
||||
public static func == <U: ManagedObjectProtocol, V: ManagedObjectProtocol>(lhs: Into<U>, rhs: Into<V>) -> Bool {
|
||||
|
||||
return lhs.entityClass == rhs.entityClass
|
||||
&& lhs.configuration == rhs.configuration
|
||||
@@ -156,26 +123,9 @@ public struct Into<T: NSManagedObject>: Hashable {
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal static var defaultConfigurationName: String {
|
||||
|
||||
return "PF_DEFAULT_CONFIGURATION_NAME"
|
||||
}
|
||||
|
||||
internal let inferStoreIfPossible: Bool
|
||||
|
||||
internal func downcast() -> Into<NSManagedObject> {
|
||||
|
||||
return Into<NSManagedObject>(
|
||||
entityClass: self.entityClass,
|
||||
configuration: self.configuration,
|
||||
inferStoreIfPossible: self.inferStoreIfPossible
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private init(entityClass: AnyClass, configuration: String?, inferStoreIfPossible: Bool) {
|
||||
internal init(entityClass: T.Type, configuration: ModelConfiguration, inferStoreIfPossible: Bool) {
|
||||
|
||||
self.entityClass = entityClass
|
||||
self.configuration = configuration
|
||||
|
||||
@@ -43,22 +43,4 @@ public extension NSManagedObject {
|
||||
|
||||
return self.managedObjectContext?.parentTransaction as? UnsafeDataTransaction
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
@nonobjc
|
||||
internal class func createInContext(_ context: NSManagedObjectContext) -> Self {
|
||||
|
||||
return self.init(
|
||||
entity: context.entityDescriptionForEntityType(self)!,
|
||||
insertInto: context
|
||||
)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func deleteFromContext() {
|
||||
|
||||
self.managedObjectContext?.delete(self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,12 +50,12 @@ public final class SynchronousDataTransaction: BaseDataTransaction {
|
||||
// MARK: BaseDataTransaction
|
||||
|
||||
/**
|
||||
Creates a new `NSManagedObject` with the specified entity type.
|
||||
Creates a new `NSManagedObject` or `ManagedObject` with the specified entity type.
|
||||
|
||||
- parameter into: the `Into` clause indicating the destination `NSManagedObject` entity type and the destination configuration
|
||||
- returns: a new `NSManagedObject` instance of the specified entity type.
|
||||
- parameter into: the `Into` clause indicating the destination `NSManagedObject` or `ManagedObject` entity type and the destination configuration
|
||||
- returns: a new `NSManagedObject` or `ManagedObject` instance of the specified entity type.
|
||||
*/
|
||||
public override func create<T: NSManagedObject>(_ into: Into<T>) -> T {
|
||||
public override func create<T: ManagedObjectProtocol>(_ into: Into<T>) -> T {
|
||||
|
||||
CoreStore.assert(
|
||||
!self.isCommitted,
|
||||
|
||||
Reference in New Issue
Block a user