WIP: objective-C fetching

This commit is contained in:
John Rommel Estropia
2016-03-27 23:02:24 +09:00
parent 789028bc58
commit b8ea7ecf01
26 changed files with 1939 additions and 198 deletions

View 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)
}
}

View 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)
}

View 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)
}
}

View File

@@ -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)
}
}
}

View 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()
}
}

View 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)
}
}

View 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()
}
}

View File

@@ -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 {

View 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)
}
}

View 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
}

View File

@@ -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
}

View File

@@ -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 {

View 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
}

View 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
}

View 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
}

View File

@@ -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
}
}

View 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)
}
}