mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-04-20 07:51:21 +02:00
WIP: objective-C fetching
This commit is contained in:
@@ -39,7 +39,7 @@ import CoreData
|
||||
let person = transaction.fetchOne(From<MyPersonEntity>("Configuration1"))
|
||||
```
|
||||
*/
|
||||
public struct From<T: NSManagedObject> {
|
||||
public struct From<T: NSManagedObject>: Hashable {
|
||||
|
||||
/**
|
||||
Initializes a `From` clause.
|
||||
@@ -299,8 +299,20 @@ public struct From<T: NSManagedObject> {
|
||||
}
|
||||
|
||||
|
||||
// MARK: Hashable
|
||||
|
||||
public var hashValue: Int {
|
||||
|
||||
return ObjectIdentifier(self.entityClass).hashValue
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal let entityClass: AnyClass
|
||||
|
||||
internal let findPersistentStores: (context: NSManagedObjectContext) -> [NSPersistentStore]?
|
||||
|
||||
internal func applyToFetchRequest(fetchRequest: NSFetchRequest, context: NSManagedObjectContext, applyAffectedStores: Bool = true) {
|
||||
|
||||
fetchRequest.entity = context.entityDescriptionForEntityClass(self.entityClass)
|
||||
@@ -317,58 +329,77 @@ public struct From<T: NSManagedObject> {
|
||||
return stores?.isEmpty == false
|
||||
}
|
||||
|
||||
internal init(entityClass: AnyClass, findPersistentStores: (context: NSManagedObjectContext) -> [NSPersistentStore]?) {
|
||||
|
||||
self.entityClass = entityClass
|
||||
self.findPersistentStores = findPersistentStores
|
||||
}
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private let entityClass: AnyClass
|
||||
|
||||
private let findPersistentStores: (context: NSManagedObjectContext) -> [NSPersistentStore]?
|
||||
|
||||
private init(entityClass: AnyClass) {
|
||||
|
||||
self.entityClass = entityClass
|
||||
self.findPersistentStores = { (context: NSManagedObjectContext) -> [NSPersistentStore]? in
|
||||
|
||||
return context.parentStack?.persistentStoresForEntityClass(entityClass)
|
||||
}
|
||||
self.init(
|
||||
entityClass: entityClass,
|
||||
findPersistentStores: { (context: NSManagedObjectContext) -> [NSPersistentStore]? in
|
||||
|
||||
return context.parentStack?.persistentStoresForEntityClass(entityClass)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private init(entityClass: AnyClass, configurations: [String?]) {
|
||||
|
||||
let configurationsSet = Set(configurations.map { $0 ?? Into.defaultConfigurationName })
|
||||
self.entityClass = entityClass
|
||||
self.findPersistentStores = { (context: NSManagedObjectContext) -> [NSPersistentStore]? in
|
||||
|
||||
return context.parentStack?.persistentStoresForEntityClass(entityClass)?.filter {
|
||||
self.init(
|
||||
entityClass: entityClass,
|
||||
findPersistentStores: { (context: NSManagedObjectContext) -> [NSPersistentStore]? in
|
||||
|
||||
return configurationsSet.contains($0.configurationName)
|
||||
return context.parentStack?.persistentStoresForEntityClass(entityClass)?.filter {
|
||||
|
||||
return configurationsSet.contains($0.configurationName)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private init(entityClass: AnyClass, storeURLs: [NSURL]) {
|
||||
|
||||
let storeURLsSet = Set(storeURLs)
|
||||
self.entityClass = entityClass
|
||||
self.findPersistentStores = { (context: NSManagedObjectContext) -> [NSPersistentStore]? in
|
||||
|
||||
return context.parentStack?.persistentStoresForEntityClass(entityClass)?.filter {
|
||||
self.init(
|
||||
entityClass: entityClass,
|
||||
findPersistentStores: { (context: NSManagedObjectContext) -> [NSPersistentStore]? in
|
||||
|
||||
return $0.URL != nil && storeURLsSet.contains($0.URL!)
|
||||
return context.parentStack?.persistentStoresForEntityClass(entityClass)?.filter {
|
||||
|
||||
return $0.URL != nil && storeURLsSet.contains($0.URL!)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private init(entityClass: AnyClass, persistentStores: [NSPersistentStore]) {
|
||||
|
||||
let persistentStores = Set(persistentStores)
|
||||
self.entityClass = entityClass
|
||||
self.findPersistentStores = { (context: NSManagedObjectContext) -> [NSPersistentStore]? in
|
||||
|
||||
return context.parentStack?.persistentStoresForEntityClass(entityClass)?.filter {
|
||||
self.init(
|
||||
entityClass: entityClass,
|
||||
findPersistentStores: { (context: NSManagedObjectContext) -> [NSPersistentStore]? in
|
||||
|
||||
return persistentStores.contains($0)
|
||||
return context.parentStack?.persistentStoresForEntityClass(entityClass)?.filter {
|
||||
|
||||
return persistentStores.contains($0)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - From: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
public func == <T: NSManagedObject, U: NSManagedObject>(lhs: From<T>, rhs: From<U>) -> Bool {
|
||||
|
||||
return lhs.entityClass == rhs.entityClass
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ import CoreData
|
||||
/**
|
||||
The `GroupBy` clause specifies that the result of a query be grouped accoording to the specified key path.
|
||||
*/
|
||||
public struct GroupBy: QueryClause {
|
||||
public struct GroupBy: QueryClause, Hashable {
|
||||
|
||||
/**
|
||||
Initializes a `GroupBy` clause with a list of key path strings
|
||||
@@ -80,4 +80,21 @@ public struct GroupBy: QueryClause {
|
||||
|
||||
fetchRequest.propertiesToGroupBy = self.keyPaths
|
||||
}
|
||||
|
||||
|
||||
// MARK: Hashable
|
||||
|
||||
public var hashValue: Int {
|
||||
|
||||
return (self.keyPaths as NSArray).hashValue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - GroupBy: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
public func == (lhs: GroupBy, rhs: GroupBy) -> Bool {
|
||||
|
||||
return lhs.keyPaths == rhs.keyPaths
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ public enum SortKey {
|
||||
/**
|
||||
The `OrderBy` clause specifies the sort order for results for a fetch or a query.
|
||||
*/
|
||||
public struct OrderBy: FetchClause, QueryClause, DeleteClause {
|
||||
public struct OrderBy: FetchClause, QueryClause, DeleteClause, Hashable {
|
||||
|
||||
/**
|
||||
Initializes a `OrderBy` clause with a list of sort descriptors
|
||||
@@ -147,4 +147,21 @@ public struct OrderBy: FetchClause, QueryClause, DeleteClause {
|
||||
|
||||
fetchRequest.sortDescriptors = self.sortDescriptors
|
||||
}
|
||||
|
||||
|
||||
// MARK: Hashable
|
||||
|
||||
public var hashValue: Int {
|
||||
|
||||
return (self.sortDescriptors as NSArray).hashValue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - OrderBy: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
public func == (lhs: OrderBy, rhs: OrderBy) -> Bool {
|
||||
|
||||
return lhs.sortDescriptors == rhs.sortDescriptors
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public prefix func !(clause: Where) -> Where {
|
||||
/**
|
||||
The `Where` clause specifies the conditions for a fetch or a query.
|
||||
*/
|
||||
public struct Where: FetchClause, QueryClause, DeleteClause {
|
||||
public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
|
||||
|
||||
/**
|
||||
Initializes a `Where` clause with an `NSPredicate`
|
||||
@@ -152,4 +152,21 @@ public struct Where: FetchClause, QueryClause, DeleteClause {
|
||||
|
||||
fetchRequest.predicate = self.predicate
|
||||
}
|
||||
|
||||
|
||||
// MARK: Hashable
|
||||
|
||||
public var hashValue: Int {
|
||||
|
||||
return self.predicate.hashValue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Where: Equatable
|
||||
|
||||
@warn_unused_result
|
||||
public func == (lhs: Where, rhs: Where) -> Bool {
|
||||
|
||||
return lhs.predicate == rhs.predicate
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user