mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-24 10:21:17 +01:00
WIP: objective-C fetching
This commit is contained in:
@@ -103,12 +103,7 @@ public extension BaseDataTransaction {
|
||||
@warn_unused_result
|
||||
public func fetchOne<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> T? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.fetchOne(from, fetchClauses)
|
||||
return self.fetchOne(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,7 +120,6 @@ public extension BaseDataTransaction {
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.fetchOne(from, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -139,12 +133,7 @@ public extension BaseDataTransaction {
|
||||
@warn_unused_result
|
||||
public func fetchAll<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> [T]? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.fetchAll(from, fetchClauses)
|
||||
return self.fetchAll(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -161,7 +150,6 @@ public extension BaseDataTransaction {
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.fetchAll(from, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -175,12 +163,7 @@ public extension BaseDataTransaction {
|
||||
@warn_unused_result
|
||||
public func fetchCount<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> Int? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.fetchCount(from, fetchClauses)
|
||||
return self.fetchCount(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,12 +194,7 @@ public extension BaseDataTransaction {
|
||||
@warn_unused_result
|
||||
public func fetchObjectID<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.fetchObjectID(from, fetchClauses)
|
||||
return self.fetchObjectID(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -233,7 +211,6 @@ public extension BaseDataTransaction {
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.fetchObjectID(from, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -247,12 +224,7 @@ public extension BaseDataTransaction {
|
||||
@warn_unused_result
|
||||
public func fetchObjectIDs<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.fetchObjectIDs(from, fetchClauses)
|
||||
return self.fetchObjectIDs(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -269,7 +241,6 @@ public extension BaseDataTransaction {
|
||||
self.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
|
||||
return self.context.fetchObjectIDs(from, fetchClauses)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -110,7 +110,6 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return self.mainContext.fetchOne(from, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -128,7 +127,6 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return self.mainContext.fetchOne(from, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -146,7 +144,6 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return self.mainContext.fetchAll(from, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -164,7 +161,6 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return self.mainContext.fetchAll(from, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -182,7 +178,6 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return self.mainContext.fetchCount(from, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -200,7 +195,6 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return self.mainContext.fetchCount(from, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -218,7 +212,6 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return self.mainContext.fetchObjectID(from, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -236,7 +229,6 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return self.mainContext.fetchObjectID(from, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -254,7 +246,6 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return self.mainContext.fetchObjectIDs(from, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -272,7 +263,6 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return self.mainContext.fetchObjectIDs(from, fetchClauses)
|
||||
}
|
||||
|
||||
@@ -293,7 +283,6 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to query from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return self.mainContext.queryValue(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
@@ -314,7 +303,6 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to query from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return self.mainContext.queryValue(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
@@ -335,7 +323,6 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to query from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return self.mainContext.queryAttributes(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
@@ -356,7 +343,6 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to query from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return self.mainContext.queryAttributes(from, selectClause, queryClauses)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,8 +31,9 @@ import CoreData
|
||||
|
||||
internal extension NSManagedObjectContext {
|
||||
|
||||
// MARK: Internal
|
||||
// MARK: Internal: Fetch Existing
|
||||
|
||||
@nonobjc
|
||||
internal func fetchExisting<T: NSManagedObject>(object: T) -> T? {
|
||||
|
||||
if object.objectID.temporaryID {
|
||||
@@ -69,11 +70,16 @@ internal extension NSManagedObjectContext {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Fetch One
|
||||
|
||||
@nonobjc
|
||||
internal func fetchOne<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> T? {
|
||||
|
||||
return self.fetchOne(from, fetchClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchOne<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> T? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
@@ -81,11 +87,13 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
fetchRequest.fetchLimit = 1
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
for clause in fetchClauses {
|
||||
|
||||
clause.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
return self.fetchOne(fetchRequest)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchOne<T: NSManagedObject>(fetchRequest: NSFetchRequest) -> T? {
|
||||
|
||||
var fetchResults: [T]?
|
||||
var fetchError: ErrorType?
|
||||
@@ -112,11 +120,16 @@ internal extension NSManagedObjectContext {
|
||||
return fetchResults?.first
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Fetch All
|
||||
|
||||
@nonobjc
|
||||
internal func fetchAll<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> [T]? {
|
||||
|
||||
return self.fetchAll(from, fetchClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchAll<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> [T]? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
@@ -124,11 +137,13 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
for clause in fetchClauses {
|
||||
|
||||
clause.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
return self.fetchAll(fetchRequest)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchAll<T: NSManagedObject>(fetchRequest: NSFetchRequest) -> [T]? {
|
||||
|
||||
var fetchResults: [T]?
|
||||
var fetchError: ErrorType?
|
||||
@@ -155,20 +170,27 @@ internal extension NSManagedObjectContext {
|
||||
return fetchResults
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Count
|
||||
|
||||
@nonobjc
|
||||
internal func fetchCount<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> Int? {
|
||||
|
||||
return self.fetchCount(from, fetchClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchCount<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> Int? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
from.applyToFetchRequest(fetchRequest, context: self)
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
for clause in fetchClauses {
|
||||
|
||||
clause.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
return self.fetchCount(fetchRequest)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchCount(fetchRequest: NSFetchRequest) -> Int? {
|
||||
|
||||
var count = 0
|
||||
var error: NSError?
|
||||
@@ -188,11 +210,16 @@ internal extension NSManagedObjectContext {
|
||||
return count
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Object ID
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectID<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? {
|
||||
|
||||
return self.fetchObjectID(from, fetchClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectID<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
@@ -200,11 +227,13 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
fetchRequest.fetchLimit = 1
|
||||
fetchRequest.resultType = .ManagedObjectIDResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
for clause in fetchClauses {
|
||||
|
||||
clause.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
return self.fetchObjectID(fetchRequest)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectID(fetchRequest: NSFetchRequest) -> NSManagedObjectID? {
|
||||
|
||||
var fetchResults: [NSManagedObjectID]?
|
||||
var fetchError: ErrorType?
|
||||
@@ -231,11 +260,16 @@ internal extension NSManagedObjectContext {
|
||||
return fetchResults?.first
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Object IDs
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectIDs<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? {
|
||||
|
||||
return self.fetchObjectIDs(from, fetchClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectIDs<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
@@ -243,11 +277,13 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .ManagedObjectIDResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
for clause in fetchClauses {
|
||||
|
||||
clause.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
return self.fetchObjectIDs(fetchRequest)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectIDs(fetchRequest: NSFetchRequest) -> [NSManagedObjectID]? {
|
||||
|
||||
var fetchResults: [NSManagedObjectID]?
|
||||
var fetchError: ErrorType?
|
||||
@@ -274,11 +310,16 @@ internal extension NSManagedObjectContext {
|
||||
return fetchResults
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Delete All
|
||||
|
||||
@nonobjc
|
||||
internal func deleteAll<T: NSManagedObject>(from: From<T>, _ deleteClauses: DeleteClause...) -> Int? {
|
||||
|
||||
return self.deleteAll(from, deleteClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func deleteAll<T: NSManagedObject>(from: From<T>, _ deleteClauses: [DeleteClause]) -> Int? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
@@ -288,11 +329,13 @@ internal extension NSManagedObjectContext {
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchRequest.returnsObjectsAsFaults = true
|
||||
fetchRequest.includesPropertyValues = false
|
||||
deleteClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
for clause in deleteClauses {
|
||||
|
||||
clause.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
return self.deleteAll(fetchRequest)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func deleteAll(fetchRequest: NSFetchRequest) -> Int? {
|
||||
|
||||
var numberOfDeletedObjects: Int?
|
||||
var fetchError: ErrorType?
|
||||
@@ -302,7 +345,7 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
do {
|
||||
|
||||
let fetchResults = try self.executeFetchRequest(fetchRequest) as? [T] ?? []
|
||||
let fetchResults = try self.executeFetchRequest(fetchRequest) as? [NSManagedObject] ?? []
|
||||
for object in fetchResults {
|
||||
|
||||
self.deleteObject(object)
|
||||
@@ -327,11 +370,16 @@ internal extension NSManagedObjectContext {
|
||||
return numberOfDeletedObjects
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Value
|
||||
|
||||
@nonobjc
|
||||
internal func queryValue<T: NSManagedObject, U: SelectValueResultType>(from: From<T>, _ selectClause: Select<U>, _ queryClauses: QueryClause...) -> U? {
|
||||
|
||||
return self.queryValue(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func queryValue<T: NSManagedObject, U: SelectValueResultType>(from: From<T>, _ selectClause: Select<U>, _ queryClauses: [QueryClause]) -> U? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
@@ -376,11 +424,16 @@ internal extension NSManagedObjectContext {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal: Attributes
|
||||
|
||||
@nonobjc
|
||||
internal func queryAttributes<T: NSManagedObject>(from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: QueryClause...) -> [[NSString: AnyObject]]? {
|
||||
|
||||
return self.queryAttributes(from, selectClause, queryClauses)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func queryAttributes<T: NSManagedObject>(from: From<T>, _ selectClause: Select<NSDictionary>, _ queryClauses: [QueryClause]) -> [[NSString: AnyObject]]? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
|
||||
206
Sources/ObjectiveC/CSBaseDataTransaction+Querying.swift
Normal file
206
Sources/ObjectiveC/CSBaseDataTransaction+Querying.swift
Normal file
@@ -0,0 +1,206 @@
|
||||
//
|
||||
// CSBaseDataTransaction+Querying.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: - CSBaseDataTransaction
|
||||
|
||||
public extension CSBaseDataTransaction {
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObject` instance in the transaction's context from a reference created from a transaction or from a different managed object context.
|
||||
|
||||
- parameter object: a reference to the object created/fetched outside the transaction
|
||||
- returns: the `NSManagedObject` instance if the object exists in the transaction, or `nil` if not found.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObject(object: NSManagedObject) -> NSManagedObject? {
|
||||
|
||||
do {
|
||||
|
||||
return try self.swift.context.existingObjectWithID(object.objectID)
|
||||
}
|
||||
catch _ {
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObject` instance in the transaction's context from an `NSManagedObjectID`.
|
||||
|
||||
- parameter objectID: the `NSManagedObjectID` for the object
|
||||
- returns: the `NSManagedObject` instance if the object exists in the transaction, or `nil` if not found.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObjectWithID(objectID: NSManagedObjectID) -> NSManagedObject? {
|
||||
|
||||
do {
|
||||
|
||||
return try self.swift.context.existingObjectWithID(objectID)
|
||||
}
|
||||
catch _ {
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObject` instances in the transaction's context from references created from a transaction or from a different managed object context.
|
||||
|
||||
- parameter objects: an array of `NSManagedObject`s created/fetched outside the transaction
|
||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObjects(objects: [NSManagedObject]) -> [NSManagedObject] {
|
||||
|
||||
return objects.flatMap { try? self.swift.context.existingObjectWithID($0.objectID) }
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObject` instances in the transaction's context from a list of `NSManagedObjectID`.
|
||||
|
||||
- parameter objectIDs: the `NSManagedObjectID` array for the objects
|
||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObjectsWithIDs(objectIDs: [NSManagedObjectID]) -> [NSManagedObject] {
|
||||
|
||||
return objectIDs.flatMap { try? self.swift.context.existingObjectWithID($0) }
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the first `NSManagedObject` instance that satisfies the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: the first `NSManagedObject` instance that satisfies the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchOneFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSManagedObject? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.swift.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
return self.swift.context.fetchOne(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches all `NSManagedObject` instances that satisfy the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: all `NSManagedObject` instances that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchAllFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> [NSManagedObject]? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.swift.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
return self.swift.context.fetchAll(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the number of `NSManagedObject`s that satisfy the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: the number `NSManagedObject`s that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchCountFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSNumber? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.swift.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
return self.swift.context.fetchCount(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchObjectIDFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSManagedObjectID? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.swift.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
return self.swift.context.fetchObjectID(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchObjectIDsFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> [NSManagedObjectID]? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.swift.isRunningInAllowedQueue(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
return self.swift.context.fetchObjectIDs(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Deletes all `NSManagedObject`s that satisfy the specified `DeleteClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter deleteClauses: a series of `DeleteClause` instances for the delete request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number of `NSManagedObject`s deleted
|
||||
*/
|
||||
@objc
|
||||
public func deleteAllFrom(from: CSFrom, deleteClauses: [CSDeleteClause]) -> NSNumber? {
|
||||
|
||||
CoreStore.assert(
|
||||
self.swift.isRunningInAllowedQueue(),
|
||||
"Attempted to delete from a \(typeName(self)) outside its designated queue."
|
||||
)
|
||||
return self.swift.context.deleteAll(from, deleteClauses)
|
||||
}
|
||||
}
|
||||
66
Sources/ObjectiveC/CSClauseTypes.swift
Normal file
66
Sources/ObjectiveC/CSClauseTypes.swift
Normal file
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// CSClauseTypes.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: - CSFetchClause
|
||||
|
||||
/**
|
||||
The `CSFetchClause` implement clauses used to configure `NSFetchRequest`s.
|
||||
*/
|
||||
@objc
|
||||
public protocol CSFetchClause {
|
||||
|
||||
@objc
|
||||
func applyToFetchRequest(fetchRequest: NSFetchRequest)
|
||||
}
|
||||
|
||||
|
||||
// MARK: - CSQueryClause
|
||||
|
||||
/**
|
||||
The `CSQueryClause` implement clauses used to configure `NSFetchRequest`s.
|
||||
*/
|
||||
@objc
|
||||
public protocol CSQueryClause {
|
||||
|
||||
@objc
|
||||
func applyToFetchRequest(fetchRequest: NSFetchRequest)
|
||||
}
|
||||
|
||||
|
||||
// MARK: - CSDeleteClause
|
||||
|
||||
/**
|
||||
The `CSDeleteClause` implement clauses used to configure `NSFetchRequest`s.
|
||||
*/
|
||||
@objc
|
||||
public protocol CSDeleteClause {
|
||||
|
||||
@objc
|
||||
func applyToFetchRequest(fetchRequest: NSFetchRequest)
|
||||
}
|
||||
168
Sources/ObjectiveC/CSCoreStore+Querying.swift
Normal file
168
Sources/ObjectiveC/CSCoreStore+Querying.swift
Normal file
@@ -0,0 +1,168 @@
|
||||
//
|
||||
// CSCoreStore+Querying.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: - CSCoreStore
|
||||
|
||||
public extension CSCoreStore {
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, fetches the `NSManagedObject` instance in the transaction's context from a reference created from a transaction or from a different managed object context.
|
||||
|
||||
- parameter object: a reference to the object created/fetched outside the transaction
|
||||
- returns: the `NSManagedObject` instance if the object exists in the transaction, or `nil` if not found.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchExistingObject(object: NSManagedObject) -> NSManagedObject? {
|
||||
|
||||
return self.defaultStack.fetchExistingObject(object)
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, fetches the `NSManagedObject` instance in the transaction's context from an `NSManagedObjectID`.
|
||||
|
||||
- parameter objectID: the `NSManagedObjectID` for the object
|
||||
- returns: the `NSManagedObject` instance if the object exists in the transaction, or `nil` if not found.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchExistingObjectWithID(objectID: NSManagedObjectID) -> NSManagedObject? {
|
||||
|
||||
return self.defaultStack.fetchExistingObjectWithID(objectID)
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, fetches the `NSManagedObject` instances in the transaction's context from references created from a transaction or from a different managed object context.
|
||||
|
||||
- parameter objects: an array of `NSManagedObject`s created/fetched outside the transaction
|
||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchExistingObjects(objects: [NSManagedObject]) -> [NSManagedObject] {
|
||||
|
||||
return self.defaultStack.fetchExistingObjects(objects)
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, fetches the `NSManagedObject` instances in the transaction's context from a list of `NSManagedObjectID`.
|
||||
|
||||
- parameter objectIDs: the `NSManagedObjectID` array for the objects
|
||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchExistingObjectsWithIDs(objectIDs: [NSManagedObjectID]) -> [NSManagedObject] {
|
||||
|
||||
return self.defaultStack.fetchExistingObjectsWithIDs(objectIDs)
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, fetches the first `NSManagedObject` instance that satisfies the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: the first `NSManagedObject` instance that satisfies the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchOneFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSManagedObject? {
|
||||
|
||||
return self.defaultStack.fetchOneFrom(from, fetchClauses: fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, fetches all `NSManagedObject` instances that satisfy the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: all `NSManagedObject` instances that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchAllFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> [NSManagedObject]? {
|
||||
|
||||
return self.defaultStack.fetchAllFrom(from, fetchClauses: fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, fetches the number of `NSManagedObject`s that satisfy the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: the number `NSManagedObject`s that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchCountFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSNumber? {
|
||||
|
||||
return self.defaultStack.fetchCountFrom(from, fetchClauses: fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, fetches the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchObjectIDFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSManagedObjectID? {
|
||||
|
||||
return self.defaultStack.fetchObjectIDFrom(from, fetchClauses: fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, fetches the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func fetchObjectIDsFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> [NSManagedObjectID]? {
|
||||
|
||||
return self.defaultStack.fetchObjectIDsFrom(from, fetchClauses: fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, deletes all `NSManagedObject`s that satisfy the specified `DeleteClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter deleteClauses: a series of `DeleteClause` instances for the delete request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number of `NSManagedObject`s deleted
|
||||
*/
|
||||
@objc
|
||||
public static func deleteAllFrom(from: CSFrom, deleteClauses: [CSDeleteClause]) -> NSNumber? {
|
||||
|
||||
return self.defaultStack.deleteAllFrom(from, deleteClauses: deleteClauses)
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public extension CSCoreStore {
|
||||
@objc
|
||||
public class var modelVersion: String {
|
||||
|
||||
return CoreStore.defaultStack.modelVersion
|
||||
return CoreStore.modelVersion
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,7 +46,7 @@ public extension CSCoreStore {
|
||||
@objc
|
||||
public static var entityClassesByName: [String: NSManagedObject.Type] {
|
||||
|
||||
return CoreStore.defaultStack.entityTypesByName
|
||||
return CoreStore.entityTypesByName
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,7 +57,7 @@ public extension CSCoreStore {
|
||||
@objc
|
||||
public static func entityClassWithName(name: String) -> NSManagedObject.Type? {
|
||||
|
||||
return CoreStore.defaultStack.entityTypesByName[name]
|
||||
return CoreStore.entityTypesByName[name]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,7 +66,7 @@ public extension CSCoreStore {
|
||||
@objc
|
||||
public static func entityDescriptionForClass(type: NSManagedObject.Type) -> NSEntityDescription? {
|
||||
|
||||
return CoreStore.defaultStack.entityDescriptionForType(type)
|
||||
return CoreStore.entityDescriptionForType(type)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,7 +82,7 @@ public extension CSCoreStore {
|
||||
|
||||
return try bridge {
|
||||
|
||||
try CoreStore.defaultStack.addStorageAndWait(InMemoryStore)
|
||||
try CoreStore.addStorageAndWait(InMemoryStore)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ public extension CSCoreStore {
|
||||
|
||||
return try bridge {
|
||||
|
||||
try CoreStore.defaultStack.addStorageAndWait(SQLiteStore)
|
||||
try CoreStore.addStorageAndWait(SQLiteStore)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ public extension CSCoreStore {
|
||||
|
||||
return try bridge {
|
||||
|
||||
try CoreStore.defaultStack.addStorageAndWait(storage.swift)
|
||||
try CoreStore.addStorageAndWait(storage.swift)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ public extension CSCoreStore {
|
||||
|
||||
return try bridge {
|
||||
|
||||
try CoreStore.defaultStack.addStorageAndWait(storage.swift)
|
||||
try CoreStore.addStorageAndWait(storage.swift)
|
||||
}
|
||||
}
|
||||
}
|
||||
105
Sources/ObjectiveC/CSCoreStore+Transaction.swift
Normal file
105
Sources/ObjectiveC/CSCoreStore+Transaction.swift
Normal file
@@ -0,0 +1,105 @@
|
||||
//
|
||||
// CSCoreStore+Transaction.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
|
||||
|
||||
|
||||
// MARK: - CSCoreStore
|
||||
|
||||
public extension CSCoreStore {
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, begins a transaction asynchronously where `NSManagedObject` creates, updates, and deletes can be made.
|
||||
|
||||
- parameter closure: the block where creates, updates, and deletes can be made to the transaction. Transaction blocks are executed serially in a background queue, and all changes are made from a concurrent `NSManagedObjectContext`.
|
||||
*/
|
||||
@objc
|
||||
public static func beginAsynchronous(closure: (transaction: CSAsynchronousDataTransaction) -> Void) {
|
||||
|
||||
return CoreStore.beginAsynchronous { (transaction) in
|
||||
|
||||
closure(transaction: transaction.objc)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, begins a transaction synchronously where `NSManagedObject` creates, updates, and deletes can be made.
|
||||
|
||||
- parameter closure: the block where creates, updates, and deletes can be made to the transaction. Transaction blocks are executed serially in a background queue, and all changes are made from a concurrent `NSManagedObjectContext`.
|
||||
- returns: a `CSSaveResult` value indicating success or failure, or `nil` if the transaction was not comitted synchronously
|
||||
*/
|
||||
@objc
|
||||
public static func beginSynchronous(closure: (transaction: CSSynchronousDataTransaction) -> Void) -> CSSaveResult? {
|
||||
|
||||
return bridge {
|
||||
|
||||
CoreStore.beginSynchronous { (transaction) in
|
||||
|
||||
closure(transaction: transaction.objc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, begins a child transaction where `NSManagedObject` creates, updates, and deletes can be made. This is useful for making temporary changes, such as partially filled forms.
|
||||
|
||||
To support "undo" methods such as `-undo`, `-redo`, and `-rollback`, use the `-beginSafeWithSupportsUndo:` method passing `YES` to the argument. Without "undo" support, calling those methods will raise an exception.
|
||||
- returns: a `CSUnsafeDataTransaction` instance where creates, updates, and deletes can be made.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func beginUnsafe() -> CSUnsafeDataTransaction {
|
||||
|
||||
return bridge {
|
||||
|
||||
CoreStore.beginUnsafe()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, begins a child transaction where `NSManagedObject` creates, updates, and deletes can be made. This is useful for making temporary changes, such as partially filled forms.
|
||||
|
||||
- prameter supportsUndo: `-undo`, `-redo`, and `-rollback` methods are only available when this parameter is `YES`, otherwise those method will raise an exception. Note that turning on Undo support may heavily impact performance especially on iOS or watchOS where memory is limited.
|
||||
- returns: a `CSUnsafeDataTransaction` instance where creates, updates, and deletes can be made.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func beginUnsafeWithSupportsUndo(supportsUndo: Bool) -> CSUnsafeDataTransaction {
|
||||
|
||||
return bridge {
|
||||
|
||||
CoreStore.beginUnsafe(supportsUndo: supportsUndo)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Refreshes all registered objects `NSManagedObject`s in the `defaultStack`.
|
||||
*/
|
||||
@objc
|
||||
public static func refreshAllObjectsAsFaults() {
|
||||
|
||||
CoreStore.refreshAllObjectsAsFaults()
|
||||
}
|
||||
}
|
||||
206
Sources/ObjectiveC/CSDataStack+Querying.swift
Normal file
206
Sources/ObjectiveC/CSDataStack+Querying.swift
Normal file
@@ -0,0 +1,206 @@
|
||||
//
|
||||
// CSDataStack+Querying.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: - CSDataStack
|
||||
|
||||
public extension CSDataStack {
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObject` instance in the transaction's context from a reference created from a transaction or from a different managed object context.
|
||||
|
||||
- parameter object: a reference to the object created/fetched outside the transaction
|
||||
- returns: the `NSManagedObject` instance if the object exists in the transaction, or `nil` if not found.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObject(object: NSManagedObject) -> NSManagedObject? {
|
||||
|
||||
do {
|
||||
|
||||
return try self.swift.mainContext.existingObjectWithID(object.objectID)
|
||||
}
|
||||
catch _ {
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObject` instance in the transaction's context from an `NSManagedObjectID`.
|
||||
|
||||
- parameter objectID: the `NSManagedObjectID` for the object
|
||||
- returns: the `NSManagedObject` instance if the object exists in the transaction, or `nil` if not found.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObjectWithID(objectID: NSManagedObjectID) -> NSManagedObject? {
|
||||
|
||||
do {
|
||||
|
||||
return try self.swift.mainContext.existingObjectWithID(objectID)
|
||||
}
|
||||
catch _ {
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObject` instances in the transaction's context from references created from a transaction or from a different managed object context.
|
||||
|
||||
- parameter objects: an array of `NSManagedObject`s created/fetched outside the transaction
|
||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObjects(objects: [NSManagedObject]) -> [NSManagedObject] {
|
||||
|
||||
return objects.flatMap { try? self.swift.mainContext.existingObjectWithID($0.objectID) }
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObject` instances in the transaction's context from a list of `NSManagedObjectID`.
|
||||
|
||||
- parameter objectIDs: the `NSManagedObjectID` array for the objects
|
||||
- returns: the `NSManagedObject` array for objects that exists in the transaction
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchExistingObjectsWithIDs(objectIDs: [NSManagedObjectID]) -> [NSManagedObject] {
|
||||
|
||||
return objectIDs.flatMap { try? self.swift.mainContext.existingObjectWithID($0) }
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the first `NSManagedObject` instance that satisfies the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: the first `NSManagedObject` instance that satisfies the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchOneFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSManagedObject? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.swift.mainContext.fetchOne(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches all `NSManagedObject` instances that satisfy the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: all `NSManagedObject` instances that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchAllFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> [NSManagedObject]? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.swift.mainContext.fetchAll(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the number of `NSManagedObject`s that satisfy the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: the number `NSManagedObject`s that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchCountFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSNumber? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.swift.mainContext.fetchCount(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for the first `NSManagedObject` that satisfies the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchObjectIDFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> NSManagedObjectID? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.swift.mainContext.fetchObjectID(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `CSFetchClause`s. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for the fetch request. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: the `NSManagedObjectID` for all `NSManagedObject`s that satisfy the specified `CSFetchClause`s
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func fetchObjectIDsFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> [NSManagedObjectID]? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to fetch from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.swift.mainContext.fetchObjectIDs(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Deletes all `NSManagedObject`s that satisfy the specified `DeleteClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
|
||||
- parameter from: a `From` clause indicating the entity type
|
||||
- parameter deleteClauses: a series of `DeleteClause` instances for the delete request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||
- returns: the number of `NSManagedObject`s deleted
|
||||
*/
|
||||
@objc
|
||||
public func deleteAllFrom(from: CSFrom, deleteClauses: [CSDeleteClause]) -> NSNumber? {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to delete from a \(typeName(self)) outside the main thread."
|
||||
)
|
||||
return self.swift.mainContext.deleteAll(from, deleteClauses)
|
||||
}
|
||||
}
|
||||
105
Sources/ObjectiveC/CSDataStack+Transaction.swift
Normal file
105
Sources/ObjectiveC/CSDataStack+Transaction.swift
Normal file
@@ -0,0 +1,105 @@
|
||||
//
|
||||
// CSDataStack+Transaction.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
|
||||
|
||||
|
||||
// MARK: - CSDataStack
|
||||
|
||||
public extension CSDataStack {
|
||||
|
||||
/**
|
||||
Begins a transaction asynchronously where `NSManagedObject` creates, updates, and deletes can be made.
|
||||
|
||||
- parameter closure: the block where creates, updates, and deletes can be made to the transaction. Transaction blocks are executed serially in a background queue, and all changes are made from a concurrent `NSManagedObjectContext`.
|
||||
*/
|
||||
@objc
|
||||
public func beginAsynchronous(closure: (transaction: CSAsynchronousDataTransaction) -> Void) {
|
||||
|
||||
return self.swift.beginAsynchronous { (transaction) in
|
||||
|
||||
closure(transaction: transaction.objc)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Begins a transaction synchronously where `NSManagedObject` creates, updates, and deletes can be made.
|
||||
|
||||
- parameter closure: the block where creates, updates, and deletes can be made to the transaction. Transaction blocks are executed serially in a background queue, and all changes are made from a concurrent `NSManagedObjectContext`.
|
||||
- returns: a `CSSaveResult` value indicating success or failure, or `nil` if the transaction was not comitted synchronously
|
||||
*/
|
||||
@objc
|
||||
public func beginSynchronous(closure: (transaction: CSSynchronousDataTransaction) -> Void) -> CSSaveResult? {
|
||||
|
||||
return bridge {
|
||||
|
||||
self.swift.beginSynchronous { (transaction) in
|
||||
|
||||
closure(transaction: transaction.objc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Begins a child transaction where `NSManagedObject` creates, updates, and deletes can be made. This is useful for making temporary changes, such as partially filled forms.
|
||||
|
||||
To support "undo" methods such as `-undo`, `-redo`, and `-rollback`, use the `-beginSafeWithSupportsUndo:` method passing `YES` to the argument. Without "undo" support, calling those methods will raise an exception.
|
||||
- returns: a `CSUnsafeDataTransaction` instance where creates, updates, and deletes can be made.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func beginUnsafe() -> CSUnsafeDataTransaction {
|
||||
|
||||
return bridge {
|
||||
|
||||
self.swift.beginUnsafe()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Begins a child transaction where `NSManagedObject` creates, updates, and deletes can be made. This is useful for making temporary changes, such as partially filled forms.
|
||||
|
||||
- prameter supportsUndo: `-undo`, `-redo`, and `-rollback` methods are only available when this parameter is `YES`, otherwise those method will raise an exception. Note that turning on Undo support may heavily impact performance especially on iOS or watchOS where memory is limited.
|
||||
- returns: a `CSUnsafeDataTransaction` instance where creates, updates, and deletes can be made.
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func beginUnsafeWithSupportsUndo(supportsUndo: Bool) -> CSUnsafeDataTransaction {
|
||||
|
||||
return bridge {
|
||||
|
||||
self.swift.beginUnsafe(supportsUndo: supportsUndo)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Refreshes all registered objects `NSManagedObject`s in the `DataStack`.
|
||||
*/
|
||||
@objc
|
||||
public func refreshAllObjectsAsFaults() {
|
||||
|
||||
self.swift.refreshAllObjectsAsFaults()
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ import CoreData
|
||||
// MARK: - CSError
|
||||
|
||||
/**
|
||||
The `CSError` provides a facade for CoreStore Objective-C constants.
|
||||
The `CSError` provides a facade for global CoreStore error declarations.
|
||||
*/
|
||||
public final class CSError: NSObject {
|
||||
|
||||
|
||||
151
Sources/ObjectiveC/CSFrom.swift
Normal file
151
Sources/ObjectiveC/CSFrom.swift
Normal file
@@ -0,0 +1,151 @@
|
||||
//
|
||||
// CSFrom.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: - CSFrom
|
||||
|
||||
/**
|
||||
The `CSFrom` serves as the Objective-C bridging type for `From`.
|
||||
*/
|
||||
@objc
|
||||
public final class CSFrom: NSObject, CoreStoreBridge {
|
||||
|
||||
/**
|
||||
Initializes a `CSFrom` clause with the specified entity class.
|
||||
```
|
||||
MyPersonEntity *people = [transaction fetchAllFrom:[CSFrom entityClass:[MyPersonEntity class]]];
|
||||
```
|
||||
- parameter entityClass: the `NSManagedObject` class type to be created
|
||||
- returns: a `CSFrom` clause with the specified entity class
|
||||
*/
|
||||
@objc
|
||||
public static func entityClass(entityClass: AnyClass) -> CSFrom {
|
||||
|
||||
return self.init(From(entityClass))
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes a `CSFrom` clause with the specified configurations.
|
||||
```
|
||||
MyPersonEntity *people = [transaction fetchAllFrom:[CSFrom entityClass:[MyPersonEntity class] configuration:@"Configuration1"]];
|
||||
```
|
||||
- parameter configuration: the `NSPersistentStore` configuration name to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
|
||||
- parameter otherConfigurations: an optional list of other configuration names to associate objects from (see `configuration` parameter)
|
||||
- returns: a `CSFrom` clause with the specified configurations
|
||||
*/
|
||||
@objc
|
||||
public static func entityClass(entityClass: AnyClass, configuration: String?) -> CSFrom {
|
||||
|
||||
return self.init(From(entityClass, configuration))
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes a `CSFrom` clause with the specified configurations.
|
||||
```
|
||||
MyPersonEntity *people = [transaction fetchAllFrom:[CSFrom entityClass:[MyPersonEntity class] configurations:@[[NSNull null], @"Configuration1"]]];
|
||||
```
|
||||
- parameter entity: the associated `NSManagedObject` entity class
|
||||
- parameter configurations: a list of `NSPersistentStore` configuration names to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `[NSNull null]` to use the default configuration.
|
||||
- returns: a `CSFrom` clause with the specified configurations
|
||||
*/
|
||||
@objc
|
||||
public static func entityClass(entityClass: AnyClass, configurations: [AnyObject]) -> CSFrom {
|
||||
|
||||
return self.init(From(entityClass, configurations.map { $0 is NSNull ? nil : ($0 as! String) }))
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes a `CSFrom` clause with the specified store URLs.
|
||||
|
||||
- parameter entity: the associated `NSManagedObject` entity class
|
||||
- parameter storeURLs: the persistent store URLs to associate objects from.
|
||||
- returns: a `CSFrom` clause with the specified store URLs
|
||||
*/
|
||||
@objc
|
||||
public static func entityClass(entityClass: AnyClass, storeURLs: [NSURL]) -> CSFrom {
|
||||
|
||||
return self.init(From(entityClass, storeURLs))
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes a `CSFrom` clause with the specified `NSPersistentStore`s.
|
||||
|
||||
- parameter entity: the associated `NSManagedObject` entity class
|
||||
- parameter persistentStores: the `NSPersistentStore`s to associate objects from.
|
||||
- returns: a `CSFrom` clause with the specified `NSPersistentStore`s
|
||||
*/
|
||||
@objc
|
||||
public static func entityClass(entityClass: AnyClass, persistentStores: [NSPersistentStore]) -> CSFrom {
|
||||
|
||||
return self.init(From(entityClass, persistentStores))
|
||||
}
|
||||
|
||||
|
||||
// MARK: NSObject
|
||||
|
||||
public override var hash: Int {
|
||||
|
||||
return self.swift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
|
||||
guard let object = object as? CSFrom else {
|
||||
|
||||
return false
|
||||
}
|
||||
return self.swift == object.swift
|
||||
}
|
||||
|
||||
|
||||
// MARK: CoreStoreBridge
|
||||
|
||||
internal let swift: From<NSManagedObject>
|
||||
|
||||
internal init<T: NSManagedObject>(_ swiftObject: From<T>) {
|
||||
|
||||
self.swift = From<NSManagedObject>(
|
||||
entityClass: swiftObject.entityClass,
|
||||
findPersistentStores: swiftObject.findPersistentStores
|
||||
)
|
||||
super.init()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - From
|
||||
|
||||
extension From: CoreStoreBridgeable {
|
||||
|
||||
// MARK: CoreStoreBridgeable
|
||||
|
||||
internal var objc: CSFrom {
|
||||
|
||||
return CSFrom(self)
|
||||
}
|
||||
}
|
||||
96
Sources/ObjectiveC/CSGroupBy.swift
Normal file
96
Sources/ObjectiveC/CSGroupBy.swift
Normal file
@@ -0,0 +1,96 @@
|
||||
//
|
||||
// CSGroupBy.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: - CSGroupBy
|
||||
|
||||
/**
|
||||
The `CSGroupBy` serves as the Objective-C bridging type for `GroupBy`.
|
||||
*/
|
||||
@objc
|
||||
public final class CSGroupBy: NSObject, CSQueryClause, CoreStoreBridge {
|
||||
|
||||
/**
|
||||
Initializes a `CSGroupBy` clause with a list of key path strings
|
||||
|
||||
- parameter keyPaths: a list of key path strings to group results with
|
||||
- returns: a `CSGroupBy` clause with a list of key path strings
|
||||
*/
|
||||
@objc
|
||||
public static func keyPaths(keyPaths: [KeyPath]) -> CSGroupBy {
|
||||
|
||||
return self.init(GroupBy(keyPaths))
|
||||
}
|
||||
|
||||
|
||||
// MARK: NSObject
|
||||
|
||||
public override var hash: Int {
|
||||
|
||||
return self.swift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
|
||||
guard let object = object as? CSGroupBy else {
|
||||
|
||||
return false
|
||||
}
|
||||
return self.swift == object.swift
|
||||
}
|
||||
|
||||
|
||||
// MARK: CSQueryClause
|
||||
|
||||
@objc
|
||||
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
|
||||
|
||||
self.swift.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
|
||||
|
||||
// MARK: CoreStoreBridge
|
||||
|
||||
internal let swift: GroupBy
|
||||
|
||||
internal init(_ swiftObject: GroupBy) {
|
||||
|
||||
self.swift = swiftObject
|
||||
super.init()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - GroupBy
|
||||
|
||||
extension GroupBy: CoreStoreBridgeable {
|
||||
|
||||
// MARK: CoreStoreBridgeable
|
||||
|
||||
internal typealias ObjCType = CSGroupBy
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
//
|
||||
// CSImportableObject.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: - CSImportableObject
|
||||
|
||||
@objc
|
||||
public protocol CSImportableObject: class, AnyObject {
|
||||
|
||||
/**
|
||||
Return the expected class of the import source. If not implemented, transactions will not validate the import source's type.
|
||||
|
||||
- returns: the expected class of the import source
|
||||
*/
|
||||
@objc
|
||||
optional static func classForImportSource() -> AnyClass
|
||||
|
||||
/**
|
||||
Return `YES` if an object should be created from `source`. Return `NO` to ignore and skip `source`. If not implemented, transactions assume `YES`.
|
||||
|
||||
- parameter source: the object to import from
|
||||
- parameter transaction: the transaction that invoked the import. Use the transaction to fetch or create related objects if needed.
|
||||
- returns: `YES` if an object should be created from `source`. Return `NO` to ignore.
|
||||
*/
|
||||
@objc
|
||||
optional static func shouldInsertFromImportSource(source: AnyObject, inTransaction transaction: CSBaseDataTransaction) -> Bool
|
||||
|
||||
/**
|
||||
Implements the actual importing of data from `source`. Implementers should pull values from `source` and assign them to the receiver's attributes. Note that throwing from this method will cause subsequent imports that are part of the same `-importObjects:sourceArray:` call to be cancelled.
|
||||
|
||||
- parameter source: the object to import from
|
||||
- parameter transaction: the transaction that invoked the import. Use the transaction to fetch or create related objects if needed.
|
||||
*/
|
||||
@objc
|
||||
func didInsertFromImportSource(source: AnyObject, inTransaction transaction: CSBaseDataTransaction) throws
|
||||
}
|
||||
@@ -37,12 +37,11 @@ public final class CSInto: NSObject, CoreStoreBridge {
|
||||
|
||||
/**
|
||||
Initializes a `CSInto` clause with the specified entity class.
|
||||
Sample Usage:
|
||||
```
|
||||
MyPersonEntity *person = [transaction create:[CSInto entityClass:[MyPersonEntity class]]];
|
||||
MyPersonEntity *person = [transaction createInto:[CSInto entityClass:[MyPersonEntity class]]];
|
||||
```
|
||||
- parameter entityClass: the `NSManagedObject` class type to be created
|
||||
- returns: a new `CSInto` with the specified entity class
|
||||
- returns: a `CSInto` clause with the specified entity class
|
||||
*/
|
||||
@objc
|
||||
public static func entityClass(entityClass: AnyClass) -> CSInto {
|
||||
@@ -51,14 +50,13 @@ public final class CSInto: NSObject, CoreStoreBridge {
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes an `CSInto` clause with the specified configuration.
|
||||
Sample Usage:
|
||||
Initializes a `CSInto` clause with the specified configuration.
|
||||
```
|
||||
MyPersonEntity *person = [transaction create:[CSInto entityClass:[MyPersonEntity class]]];
|
||||
MyPersonEntity *person = [transaction createInto:[CSInto entityClass:[MyPersonEntity class]]];
|
||||
```
|
||||
- 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.
|
||||
- returns: a new `CSInto` with the specified configuration
|
||||
- returns: a `CSInto` clause with the specified configuration
|
||||
*/
|
||||
@objc
|
||||
public static func entityClass(entityClass: AnyClass, configuration: String?) -> CSInto {
|
||||
|
||||
108
Sources/ObjectiveC/CSOrderBy.swift
Normal file
108
Sources/ObjectiveC/CSOrderBy.swift
Normal file
@@ -0,0 +1,108 @@
|
||||
//
|
||||
// CSOrderBy.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: - CSOrderBy
|
||||
|
||||
/**
|
||||
The `CSOrderBy` serves as the Objective-C bridging type for `OrderBy`.
|
||||
*/
|
||||
@objc
|
||||
public final class CSOrderBy: NSObject, CSFetchClause, CSQueryClause, CSDeleteClause, CoreStoreBridge {
|
||||
|
||||
/**
|
||||
Initializes a `CSOrderBy` clause with a list of sort descriptors
|
||||
|
||||
- parameter sortDescriptors: a series of `NSSortDescriptor`s
|
||||
- returns: a `CSOrderBy` clause with a list of sort descriptors
|
||||
*/
|
||||
@objc
|
||||
public static func sortDescriptors(sortDescriptors: [NSSortDescriptor]) -> CSOrderBy {
|
||||
|
||||
return self.init(OrderBy(sortDescriptors))
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes a `CSOrderBy` clause with a single sort descriptor
|
||||
|
||||
- parameter sortDescriptor: a `NSSortDescriptor`
|
||||
- returns: a `CSOrderBy` clause with a single sort descriptor
|
||||
*/
|
||||
@objc
|
||||
public static func sortDescriptor(sortDescriptor: NSSortDescriptor) -> CSOrderBy {
|
||||
|
||||
return self.init(OrderBy(sortDescriptor))
|
||||
}
|
||||
|
||||
|
||||
// MARK: NSObject
|
||||
|
||||
public override var hash: Int {
|
||||
|
||||
return self.swift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
|
||||
guard let object = object as? CSOrderBy else {
|
||||
|
||||
return false
|
||||
}
|
||||
return self.swift == object.swift
|
||||
}
|
||||
|
||||
|
||||
// MARK: CSFetchClause, CSQueryClause, CSDeleteClause
|
||||
|
||||
@objc
|
||||
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
|
||||
|
||||
self.swift.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
|
||||
|
||||
// MARK: CoreStoreBridge
|
||||
|
||||
internal let swift: OrderBy
|
||||
|
||||
internal init(_ swiftObject: OrderBy) {
|
||||
|
||||
self.swift = swiftObject
|
||||
super.init()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - OrderBy
|
||||
|
||||
extension OrderBy: CoreStoreBridgeable {
|
||||
|
||||
// MARK: CoreStoreBridgeable
|
||||
|
||||
internal typealias ObjCType = CSOrderBy
|
||||
}
|
||||
79
Sources/ObjectiveC/CSTweak.swift
Normal file
79
Sources/ObjectiveC/CSTweak.swift
Normal file
@@ -0,0 +1,79 @@
|
||||
//
|
||||
// CSTweak.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: - CSTweak
|
||||
|
||||
/**
|
||||
The `CSTweak` serves as the Objective-C bridging type for `Tweak`.
|
||||
*/
|
||||
@objc
|
||||
public final class CSTweak: NSObject, CSFetchClause, CSQueryClause, CSDeleteClause, CoreStoreBridge {
|
||||
|
||||
/**
|
||||
Initializes a `CSTweak` clause with a closure where the `NSFetchRequest` may be configured.
|
||||
|
||||
- parameter customization: a list of key path strings to group results with
|
||||
- returns: a `CSTweak` clause with a closure where the `NSFetchRequest` may be configured
|
||||
*/
|
||||
@objc
|
||||
public static func customization(customization: (fetchRequest: NSFetchRequest) -> Void) -> CSTweak {
|
||||
|
||||
return self.init(Tweak(customization))
|
||||
}
|
||||
|
||||
|
||||
// MARK: CSFetchClause, CSQueryClause, CSDeleteClause
|
||||
|
||||
@objc
|
||||
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
|
||||
|
||||
self.swift.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
|
||||
|
||||
// MARK: CoreStoreBridge
|
||||
|
||||
internal let swift: Tweak
|
||||
|
||||
internal init(_ swiftObject: Tweak) {
|
||||
|
||||
self.swift = swiftObject
|
||||
super.init()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Tweak
|
||||
|
||||
extension Tweak: CoreStoreBridgeable {
|
||||
|
||||
// MARK: CoreStoreBridgeable
|
||||
|
||||
internal typealias ObjCType = CSTweak
|
||||
}
|
||||
147
Sources/ObjectiveC/CSWhere.swift
Normal file
147
Sources/ObjectiveC/CSWhere.swift
Normal file
@@ -0,0 +1,147 @@
|
||||
//
|
||||
// CSWhere.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: - CSWhere
|
||||
|
||||
/**
|
||||
The `CSWhere` serves as the Objective-C bridging type for `Where`.
|
||||
*/
|
||||
@objc
|
||||
public final class CSWhere: NSObject, CSFetchClause, CSQueryClause, CSDeleteClause, CoreStoreBridge {
|
||||
|
||||
/**
|
||||
Initializes a `CSWhere` clause with an `NSPredicate`
|
||||
|
||||
- parameter predicate: the `NSPredicate` for the fetch or query
|
||||
- returns: a `CSWhere` clause with an `NSPredicate`
|
||||
*/
|
||||
@objc
|
||||
public static func predicate(predicate: NSPredicate) -> CSWhere {
|
||||
|
||||
return self.init(Where(predicate))
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes a `CSWhere` clause with a predicate that always evaluates to the specified boolean value
|
||||
|
||||
- parameter value: the boolean value for the predicate
|
||||
- returns: a `CSWhere` clause with a predicate that always evaluates to the specified boolean value
|
||||
*/
|
||||
@objc
|
||||
public static func value(value: Bool) -> CSWhere {
|
||||
|
||||
return self.init(Where(value))
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes a `CSWhere` clause with a predicate using the specified string format and arguments
|
||||
|
||||
- parameter format: the format string for the predicate
|
||||
- parameter argumentArray: the arguments for `format`
|
||||
- returns: a `CSWhere` clause with a predicate using the specified string format and arguments
|
||||
*/
|
||||
@objc
|
||||
public static func format(format: String, argumentArray: [NSObject]?) -> CSWhere {
|
||||
|
||||
return self.init(Where(format, argumentArray: argumentArray))
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes a `CSWhere` clause that compares equality
|
||||
|
||||
- parameter keyPath: the keyPath to compare with
|
||||
- parameter value: the arguments for the `==` operator
|
||||
- returns: a `CSWhere` clause that compares equality
|
||||
*/
|
||||
@objc
|
||||
public static func keyPath(keyPath: KeyPath, isEqualTo value: NSObject?) -> CSWhere {
|
||||
|
||||
return self.init(Where(keyPath, isEqualTo: value))
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes a `CSWhere` clause that compares membership
|
||||
|
||||
- parameter keyPath: the keyPath to compare with
|
||||
- parameter list: the array to check membership of
|
||||
- returns: a `CSWhere` clause that compares membership
|
||||
*/
|
||||
@objc
|
||||
public static func keyPath(keyPath: KeyPath, isMemberOf list: NSArray) -> CSWhere {
|
||||
|
||||
return self.init(Where(keyPath, isMemberOf: list))
|
||||
}
|
||||
|
||||
|
||||
// MARK: NSObject
|
||||
|
||||
public override var hash: Int {
|
||||
|
||||
return self.swift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
|
||||
guard let object = object as? CSWhere else {
|
||||
|
||||
return false
|
||||
}
|
||||
return self.swift == object.swift
|
||||
}
|
||||
|
||||
|
||||
// MARK: CSFetchClause, CSQueryClause, CSDeleteClause
|
||||
|
||||
@objc
|
||||
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
|
||||
|
||||
self.swift.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
|
||||
|
||||
// MARK: CoreStoreBridge
|
||||
|
||||
internal let swift: Where
|
||||
|
||||
internal init(_ swiftObject: Where) {
|
||||
|
||||
self.swift = swiftObject
|
||||
super.init()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Where
|
||||
|
||||
extension Where: CoreStoreBridgeable {
|
||||
|
||||
// MARK: CoreStoreBridgeable
|
||||
|
||||
internal typealias ObjCType = CSWhere
|
||||
}
|
||||
@@ -92,4 +92,19 @@ internal func bridge(@noescape closure: () throws -> Void) throws {
|
||||
}
|
||||
}
|
||||
|
||||
internal func bridge<T>(error: NSErrorPointer, @noescape _ closure: () throws -> T) -> T? {
|
||||
|
||||
do {
|
||||
|
||||
let result = try closure()
|
||||
error.memory = nil
|
||||
return result
|
||||
}
|
||||
catch let swiftError {
|
||||
|
||||
error.memory = swiftError.objc
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
112
Sources/ObjectiveC/NSManagedObjectContext+ObjectiveC.swift
Normal file
112
Sources/ObjectiveC/NSManagedObjectContext+ObjectiveC.swift
Normal file
@@ -0,0 +1,112 @@
|
||||
//
|
||||
// NSManagedObjectContext+ObjectiveC.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: - NSManagedObjectContext
|
||||
|
||||
internal extension NSManagedObjectContext {
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
@nonobjc
|
||||
internal func fetchOne(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> NSManagedObject? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
from.swift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 1
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
return self.fetchOne(fetchRequest)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchAll(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> [NSManagedObject]? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
from.swift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
return self.fetchAll(fetchRequest)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchCount(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> Int? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
from.swift.applyToFetchRequest(fetchRequest, context: self)
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
return self.fetchCount(fetchRequest)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectID(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> NSManagedObjectID? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
from.swift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 1
|
||||
fetchRequest.resultType = .ManagedObjectIDResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
return self.fetchObjectID(fetchRequest)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func fetchObjectIDs(from: CSFrom, _ fetchClauses: [CSFetchClause]) -> [NSManagedObjectID]? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
from.swift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .ManagedObjectIDResultType
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
return self.fetchObjectIDs(fetchRequest)
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
internal func deleteAll(from: CSFrom, _ deleteClauses: [CSDeleteClause]) -> Int? {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
from.swift.applyToFetchRequest(fetchRequest, context: self)
|
||||
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchRequest.returnsObjectsAsFaults = true
|
||||
fetchRequest.includesPropertyValues = false
|
||||
deleteClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
|
||||
return self.deleteAll(fetchRequest)
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,7 @@ public extension CoreStore {
|
||||
}
|
||||
|
||||
/**
|
||||
Refreshes all registered objects `NSManagedObject`s in the `DataStack`.
|
||||
Refreshes all registered objects `NSManagedObject`s in the `defaultStack`.
|
||||
*/
|
||||
public static func refreshAllObjectsAsFaults() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user