mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-04-29 20:27:11 +02:00
WIP: prototype for ManagedObjectProtocol
This commit is contained in:
@@ -66,7 +66,7 @@ internal final class CoreStoreFetchedResultsController: NSFetchedResultsControll
|
||||
}
|
||||
else {
|
||||
|
||||
guard let from = (fetchRequest.entity.flatMap { $0.managedObjectClassName }).flatMap(NSClassFromString).flatMap(From.init) else {
|
||||
guard let from = (fetchRequest.entity.flatMap { $0.managedObjectClassName }).flatMap(NSClassFromString).flatMap({ From<T>($0 as! T.Type) }) else {
|
||||
|
||||
CoreStore.abort("Attempted to create a \(CoreStoreFetchedResultsController.self) without a \(cs_typeName(From<T>.self)) clause or an \(cs_typeName(NSEntityDescription.self)).")
|
||||
}
|
||||
|
||||
@@ -54,25 +54,6 @@ internal extension NSManagedObjectContext {
|
||||
}
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func entityDescriptionForEntityType(_ entity: NSManagedObject.Type) -> NSEntityDescription? {
|
||||
|
||||
return self.entityDescriptionForEntityClass(entity)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func entityDescriptionForEntityClass(_ entity: AnyClass) -> NSEntityDescription? {
|
||||
|
||||
guard let entityName = self.parentStack?.entityNameForEntityClass(entity) else {
|
||||
|
||||
return nil
|
||||
}
|
||||
return NSEntityDescription.entity(
|
||||
forEntityName: entityName,
|
||||
in: self
|
||||
)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func setupForCoreStoreWithContextName(_ contextName: String) {
|
||||
|
||||
|
||||
@@ -95,14 +95,13 @@ extension NSManagedObjectContext: FetchableSource, QueryableSource {
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
public func fetchOne<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> T? {
|
||||
|
||||
public func fetchOne<T: ManagedObjectProtocol>(_ from: From<T>, _ fetchClauses: FetchClause...) -> T? {
|
||||
|
||||
return self.fetchOne(from, fetchClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
public func fetchOne<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
|
||||
public func fetchOne<T: ManagedObjectProtocol>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
|
||||
|
||||
let fetchRequest = CoreStoreFetchRequest()
|
||||
let storeFound = from.applyToFetchRequest(fetchRequest, context: self)
|
||||
@@ -116,6 +115,7 @@ extension NSManagedObjectContext: FetchableSource, QueryableSource {
|
||||
return nil
|
||||
}
|
||||
return self.fetchOne(fetchRequest.dynamicCast())
|
||||
.flatMap(from.entityClass.cs_from)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
|
||||
@@ -157,22 +157,50 @@ internal extension NSManagedObjectModel {
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func entityNameForClass(_ entityClass: AnyClass) -> String {
|
||||
internal var entityDescriptionsByEntityIdentifier: [EntityIdentifier: NSEntityDescription] {
|
||||
|
||||
return self.entityNameMapping[NSStringFromClass(entityClass)]!
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func entityTypesMapping() -> [String: NSManagedObject.Type] {
|
||||
|
||||
var mapping = [String: NSManagedObject.Type]()
|
||||
self.entityNameMapping.forEach { (className, entityName) in
|
||||
if let mapping: NSDictionary = cs_getAssociatedObjectForKey(&PropertyKeys.objectClassNamesByEntityName, inObject: self) {
|
||||
|
||||
mapping[entityName] = (NSClassFromString(className)! as! NSManagedObject.Type)
|
||||
return mapping as! [EntityIdentifier: NSEntityDescription]
|
||||
}
|
||||
|
||||
var mapping: [EntityIdentifier: NSEntityDescription] = [:]
|
||||
self.entities.forEach { (entityDescription) in
|
||||
|
||||
let entityIdentifier = EntityIdentifier(entityDescription)
|
||||
mapping[entityIdentifier] = entityDescription
|
||||
}
|
||||
cs_setAssociatedCopiedObject(
|
||||
mapping as NSDictionary,
|
||||
forKey: &PropertyKeys.objectClassNamesByEntityName,
|
||||
inObject: self
|
||||
)
|
||||
return mapping
|
||||
}
|
||||
|
||||
// TODO: remove
|
||||
// @nonobjc
|
||||
// internal func entityNames(for type: NSManagedObject.Type) -> Set<EntityName> {
|
||||
//
|
||||
// let className = NSStringFromClass(type)
|
||||
// return Set(
|
||||
// self.objectClassNamesByEntityName
|
||||
// .filter({ $0.value == className })
|
||||
// .map({ $0.key })
|
||||
// )
|
||||
// }
|
||||
//
|
||||
// @nonobjc
|
||||
// internal func entityTypesMapping() -> [EntityName: NSManagedObject.Type] {
|
||||
//
|
||||
// var mapping = [EntityName: NSManagedObject.Type]()
|
||||
// self.objectClassNamesByEntityName.forEach { (entityName, className) in
|
||||
//
|
||||
// mapping[entityName] = (NSClassFromString(className)! as! NSManagedObject.Type)
|
||||
// }
|
||||
// return mapping
|
||||
// }
|
||||
|
||||
@nonobjc
|
||||
internal func mergedModels() -> [NSManagedObjectModel] {
|
||||
|
||||
@@ -256,39 +284,9 @@ internal extension NSManagedObjectModel {
|
||||
}
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
private var entityNameMapping: [String: String] {
|
||||
|
||||
get {
|
||||
|
||||
if let mapping: NSDictionary = cs_getAssociatedObjectForKey(&PropertyKeys.entityNameMapping, inObject: self) {
|
||||
|
||||
return mapping as! [String: String]
|
||||
}
|
||||
|
||||
var mapping = [String: String]()
|
||||
self.entities.forEach { // TODO: use AnyEntity as mapping key
|
||||
|
||||
guard let entityName = $0.name else {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
let className = $0.managedObjectClassName
|
||||
mapping[className!] = entityName
|
||||
}
|
||||
cs_setAssociatedCopiedObject(
|
||||
mapping as NSDictionary,
|
||||
forKey: &PropertyKeys.entityNameMapping,
|
||||
inObject: self
|
||||
)
|
||||
return mapping
|
||||
}
|
||||
}
|
||||
|
||||
private struct PropertyKeys {
|
||||
|
||||
static var entityNameMapping: Void?
|
||||
static var objectClassNamesByEntityName: Void?
|
||||
|
||||
static var modelVersionFileURL: Void?
|
||||
static var modelVersions: Void?
|
||||
|
||||
@@ -72,7 +72,7 @@ internal extension NSPersistentStoreCoordinator {
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func addPersistentStoreSynchronously(_ storeType: String, configuration: String?, URL storeURL: URL?, options: [NSObject : AnyObject]?) throws -> NSPersistentStore {
|
||||
internal func addPersistentStoreSynchronously(_ storeType: String, configuration: ModelConfiguration, URL storeURL: URL?, options: [NSObject : AnyObject]?) throws -> NSPersistentStore {
|
||||
|
||||
var store: NSPersistentStore?
|
||||
var storeError: NSError?
|
||||
|
||||
Reference in New Issue
Block a user