WIP: simpler bridging

This commit is contained in:
John Estropia
2016-03-24 21:22:58 +09:00
parent 09708e587c
commit 90369cf994
14 changed files with 765 additions and 546 deletions

View File

@@ -28,11 +28,189 @@ import Foundation
// MARK: - CSAsynchronousDataTransaction
/**
The `CSAsynchronousDataTransaction` serves as the Objective-C bridging type for `AsynchronousDataTransaction`.
*/
@objc
public final class CSAsynchronousDataTransaction: CSBaseDataTransaction {
internal init(_ swiftObject: AsynchronousDataTransaction) {
// /**
// Saves the transaction changes. This method should not be used after the `commit()` method was already called once.
//
// - parameter completion: the block executed after the save completes. Success or failure is reported by the `SaveResult` argument of the block.
// */
// public func commit(completion: (result: SaveResult) -> Void = { _ in }) {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to commit a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to commit a \(typeName(self)) more than once."
// )
//
// self.isCommitted = true
// let group = GCDGroup()
// group.enter()
// self.context.saveAsynchronouslyWithCompletion { (result) -> Void in
//
// self.result = result
// completion(result: result)
// group.leave()
// }
// group.wait()
// }
//
// /**
// Begins a child transaction synchronously where NSManagedObject creates, updates, and deletes can be made. This method should not be used after the `commit()` method was already called once.
//
// - 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 `SaveResult` value indicating success or failure, or `nil` if the transaction was not comitted synchronously
// */
// public func beginSynchronous(closure: (transaction: SynchronousDataTransaction) -> Void) -> SaveResult? {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to begin a child transaction from a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to begin a child transaction from an already committed \(typeName(self))."
// )
//
// return SynchronousDataTransaction(
// mainContext: self.context,
// queue: self.childTransactionQueue,
// closure: closure).performAndWait()
// }
//
//
// // MARK: BaseDataTransaction
//
// /**
// Creates a new `NSManagedObject` with the specified entity type.
//
// - parameter into: the `Into` clause indicating the destination `NSManagedObject` entity type and the destination configuration
// - returns: a new `NSManagedObject` instance of the specified entity type.
// */
// public override func create<T: NSManagedObject>(into: Into<T>) -> T {
//
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to create an entity of type \(typeName(T)) from an already committed \(typeName(self))."
// )
//
// return super.create(into)
// }
//
// /**
// Returns an editable proxy of a specified `NSManagedObject`. This method should not be used after the `commit()` method was already called once.
//
// - parameter object: the `NSManagedObject` type to be edited
// - returns: an editable proxy for the specified `NSManagedObject`.
// */
// @warn_unused_result
// public override func edit<T: NSManagedObject>(object: T?) -> T? {
//
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to update an entity of type \(typeName(object)) from an already committed \(typeName(self))."
// )
//
// return super.edit(object)
// }
//
// /**
// Returns an editable proxy of the object with the specified `NSManagedObjectID`. This method should not be used after the `commit()` method was already called once.
//
// - parameter into: an `Into` clause specifying the entity type
// - parameter objectID: the `NSManagedObjectID` for the object to be edited
// - returns: an editable proxy for the specified `NSManagedObject`.
// */
// @warn_unused_result
// public override func edit<T: NSManagedObject>(into: Into<T>, _ objectID: NSManagedObjectID) -> T? {
//
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to update an entity of type \(typeName(T)) from an already committed \(typeName(self))."
// )
//
// return super.edit(into, objectID)
// }
//
// /**
// Deletes a specified `NSManagedObject`. This method should not be used after the `commit()` method was already called once.
//
// - parameter object: the `NSManagedObject` type to be deleted
// */
// public override func delete(object: NSManagedObject?) {
//
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to delete an entity of type \(typeName(object)) from an already committed \(typeName(self))."
// )
//
// super.delete(object)
// }
//
// /**
// Deletes the specified `NSManagedObject`s.
//
// - parameter object1: the `NSManagedObject` type to be deleted
// - parameter object2: another `NSManagedObject` type to be deleted
// - parameter objects: other `NSManagedObject`s type to be deleted
// */
// public override func delete(object1: NSManagedObject?, _ object2: NSManagedObject?, _ objects: NSManagedObject?...) {
//
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to delete an entities from an already committed \(typeName(self))."
// )
//
// super.delete(([object1, object2] + objects).flatMap { $0 })
// }
//
// /**
// Deletes the specified `NSManagedObject`s.
//
// - parameter objects: the `NSManagedObject`s type to be deleted
// */
// public override func delete<S: SequenceType where S.Generator.Element: NSManagedObject>(objects: S) {
//
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to delete an entities from an already committed \(typeName(self))."
// )
//
// super.delete(objects)
// }
// MARK: CoreStoreBridge
internal override var swift: AsynchronousDataTransaction {
return super.swift as! AsynchronousDataTransaction
}
public required init(_ swiftObject: AsynchronousDataTransaction) {
super.init(swiftObject)
}
required public init(_ swiftObject: BaseDataTransaction) {
fatalError("init has not been implemented")
}
}
// MARK: - AsynchronousDataTransaction
extension AsynchronousDataTransaction {
// MARK: CoreStoreBridgeable
internal typealias ObjCType = CSAsynchronousDataTransaction
}

View File

@@ -26,24 +26,13 @@
import Foundation
// MARK: - DataStack
extension BaseDataTransaction {
var objc: CSBaseDataTransaction {
return CSBaseDataTransaction(self)
}
}
// MARK: - CSBaseDataTransaction
/**
The `CSBaseDataTransaction` serves as the Objective-C bridging type for `BaseDataTransaction`.
*/
@objc
public class CSBaseDataTransaction: NSObject {
public class CSBaseDataTransaction: NSObject, CoreStoreBridge {
// MARK: Object management
@@ -59,13 +48,13 @@ public class CSBaseDataTransaction: NSObject {
/**
Creates a new `NSManagedObject` with the specified entity type.
- parameter into: the `Into` clause indicating the destination `NSManagedObject` entity type and the destination configuration
- parameter into: the `CSInto` clause indicating the destination `NSManagedObject` entity type and the destination configuration
- returns: a new `NSManagedObject` instance of the specified entity type.
*/
@objc
public func create(into into: NSManagedObject.Type) -> NSManagedObject {
public func createInto(into: CSInto) -> NSManagedObject {
return self.swift.create(Into(into))
return self.swift.create(into.swift)
}
/**
@@ -76,329 +65,209 @@ public class CSBaseDataTransaction: NSObject {
*/
@objc
@warn_unused_result
public func edit(object: NSManagedObject?) -> NSManagedObject? {
public func editObject(object: NSManagedObject?) -> NSManagedObject? {
return self.swift.edit(object)
}
// /**
// Returns an editable proxy of the object with the specified `NSManagedObjectID`.
//
// - parameter into: an `Into` clause specifying the entity type
// - parameter objectID: the `NSManagedObjectID` for the object to be edited
// - returns: an editable proxy for the specified `NSManagedObject`.
// */
// @warn_unused_result
// public func edit<T: NSManagedObject>(into: Into<T>, _ objectID: NSManagedObjectID) -> T? {
//
// CoreStore.assert(
// self.isRunningInAllowedQueue(),
// "Attempted to update an entity of type \(typeName(T)) outside its designated queue."
// )
// CoreStore.assert(
// into.inferStoreIfPossible
// || (into.configuration ?? Into.defaultConfigurationName) == objectID.persistentStore?.configurationName,
// "Attempted to update an entity of type \(typeName(T)) but the specified persistent store do not match the `NSManagedObjectID`."
// )
// return self.fetchExisting(objectID) as? T
// }
//
// /**
// Deletes a specified `NSManagedObject`.
//
// - parameter object: the `NSManagedObject` to be deleted
// */
// public func delete(object: NSManagedObject?) {
//
// CoreStore.assert(
// self.isRunningInAllowedQueue(),
// "Attempted to delete an entity outside its designated queue."
// )
// guard let object = object else {
//
// return
// }
// self.context.fetchExisting(object)?.deleteFromContext()
// }
//
// /**
// Deletes the specified `NSManagedObject`s.
//
// - parameter object1: the `NSManagedObject` to be deleted
// - parameter object2: another `NSManagedObject` to be deleted
// - parameter objects: other `NSManagedObject`s to be deleted
// */
// public func delete(object1: NSManagedObject?, _ object2: NSManagedObject?, _ objects: NSManagedObject?...) {
//
// self.delete(([object1, object2] + objects).flatMap { $0 })
// }
//
// /**
// Deletes the specified `NSManagedObject`s.
//
// - parameter objects: the `NSManagedObject`s to be deleted
// */
// public func delete<S: SequenceType where S.Generator.Element: NSManagedObject>(objects: S) {
//
// CoreStore.assert(
// self.isRunningInAllowedQueue(),
// "Attempted to delete entities outside their designated queue."
// )
//
// let context = self.context
// objects.forEach { context.fetchExisting($0)?.deleteFromContext() }
// }
//
// /**
// Refreshes all registered objects `NSManagedObject`s in the transaction.
// */
// public func refreshAllObjectsAsFaults() {
//
// CoreStore.assert(
// self.isRunningInAllowedQueue(),
// "Attempted to refresh entities outside their designated queue."
// )
//
// self.context.refreshAllObjectsAsFaults()
// }
//
//
// // MARK: Inspecting Pending Objects
//
// /**
// Returns all pending `NSManagedObject`s that were inserted to the transaction. This method should not be called after the `commit()` method was called.
//
// - returns: a `Set` of pending `NSManagedObject`s that were inserted to the transaction.
// */
// public func insertedObjects() -> Set<NSManagedObject> {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to access inserted objects from a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to access inserted objects from an already committed \(typeName(self))."
// )
//
// return self.context.insertedObjects
// }
//
// /**
// Returns all pending `NSManagedObject`s of the specified type that were inserted to the transaction. This method should not be called after the `commit()` method was called.
//
// - parameter entity: the `NSManagedObject` subclass to filter
// - returns: a `Set` of pending `NSManagedObject`s of the specified type that were inserted to the transaction.
// */
// public func insertedObjects<T: NSManagedObject>(entity: T.Type) -> Set<T> {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to access inserted objects from a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to access inserted objects from an already committed \(typeName(self))."
// )
//
// return Set(self.context.insertedObjects.flatMap { $0 as? T })
// }
//
// /**
// Returns all pending `NSManagedObjectID`s that were inserted to the transaction. This method should not be called after the `commit()` method was called.
//
// - returns: a `Set` of pending `NSManagedObjectID`s that were inserted to the transaction.
// */
// public func insertedObjectIDs() -> Set<NSManagedObjectID> {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to access inserted object IDs from a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to access inserted objects IDs from an already committed \(typeName(self))."
// )
//
// return Set(self.context.insertedObjects.map { $0.objectID })
// }
//
// /**
// Returns all pending `NSManagedObjectID`s of the specified type that were inserted to the transaction. This method should not be called after the `commit()` method was called.
//
// - parameter entity: the `NSManagedObject` subclass to filter
// - returns: a `Set` of pending `NSManagedObjectID`s of the specified type that were inserted to the transaction.
// */
// public func insertedObjectIDs<T: NSManagedObject>(entity: T.Type) -> Set<NSManagedObjectID> {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to access inserted object IDs from a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to access inserted objects IDs from an already committed \(typeName(self))."
// )
//
// return Set(self.context.insertedObjects.flatMap { $0 as? T }.map { $0.objectID })
// }
//
// /**
// Returns all pending `NSManagedObject`s that were updated in the transaction. This method should not be called after the `commit()` method was called.
//
// - returns: a `Set` of pending `NSManagedObject`s that were updated to the transaction.
// */
// public func updatedObjects() -> Set<NSManagedObject> {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to access updated objects from a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to access updated objects from an already committed \(typeName(self))."
// )
//
// return self.context.updatedObjects
// }
//
// /**
// Returns all pending `NSManagedObject`s of the specified type that were updated in the transaction. This method should not be called after the `commit()` method was called.
//
// - parameter entity: the `NSManagedObject` subclass to filter
// - returns: a `Set` of pending `NSManagedObject`s of the specified type that were updated in the transaction.
// */
// public func updatedObjects<T: NSManagedObject>(entity: T.Type) -> Set<T> {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to access updated objects from a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to access updated objects from an already committed \(typeName(self))."
// )
//
// return Set(self.context.updatedObjects.flatMap { $0 as? T })
// }
//
// /**
// Returns all pending `NSManagedObjectID`s that were updated in the transaction. This method should not be called after the `commit()` method was called.
//
// - returns: a `Set` of pending `NSManagedObjectID`s that were updated in the transaction.
// */
// public func updatedObjectIDs() -> Set<NSManagedObjectID> {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to access updated object IDs from a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to access updated object IDs from an already committed \(typeName(self))."
// )
//
// return Set(self.context.updatedObjects.map { $0.objectID })
// }
//
// /**
// Returns all pending `NSManagedObjectID`s of the specified type that were updated in the transaction. This method should not be called after the `commit()` method was called.
//
// - parameter entity: the `NSManagedObject` subclass to filter
// - returns: a `Set` of pending `NSManagedObjectID`s of the specified type that were updated in the transaction.
// */
// public func updatedObjectIDs<T: NSManagedObject>(entity: T.Type) -> Set<NSManagedObjectID> {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to access updated object IDs from a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to access updated object IDs from an already committed \(typeName(self))."
// )
//
// return Set(self.context.updatedObjects.flatMap { $0 as? T }.map { $0.objectID })
// }
//
// /**
// Returns all pending `NSManagedObject`s that were deleted from the transaction. This method should not be called after the `commit()` method was called.
//
// - returns: a `Set` of pending `NSManagedObject`s that were deleted from the transaction.
// */
// public func deletedObjects() -> Set<NSManagedObject> {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to access deleted objects from a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to access deleted objects from an already committed \(typeName(self))."
// )
//
// return self.context.deletedObjects
// }
//
// /**
// Returns all pending `NSManagedObject`s of the specified type that were deleted from the transaction. This method should not be called after the `commit()` method was called.
//
// - parameter entity: the `NSManagedObject` subclass to filter
// - returns: a `Set` of pending `NSManagedObject`s of the specified type that were deleted from the transaction.
// */
// public func deletedObjects<T: NSManagedObject>(entity: T.Type) -> Set<T> {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to access deleted objects from a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to access deleted objects from an already committed \(typeName(self))."
// )
//
// return Set(self.context.deletedObjects.flatMap { $0 as? T })
// }
//
// /**
// Returns all pending `NSManagedObjectID`s of the specified type that were deleted from the transaction. This method should not be called after the `commit()` method was called.
//
// - parameter entity: the `NSManagedObject` subclass to filter
// - returns: a `Set` of pending `NSManagedObjectID`s of the specified type that were deleted from the transaction.
// */
// public func deletedObjectIDs() -> Set<NSManagedObjectID> {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to access deleted object IDs from a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to access deleted object IDs from an already committed \(typeName(self))."
// )
//
// return Set(self.context.deletedObjects.map { $0.objectID })
// }
//
// /**
// Returns all pending `NSManagedObjectID`s of the specified type that were deleted from the transaction. This method should not be called after the `commit()` method was called.
//
// - parameter entity: the `NSManagedObject` subclass to filter
// - returns: a `Set` of pending `NSManagedObjectID`s of the specified type that were deleted from the transaction.
// */
// public func deletedObjectIDs<T: NSManagedObject>(entity: T.Type) -> Set<NSManagedObjectID> {
//
// CoreStore.assert(
// self.transactionQueue.isCurrentExecutionContext(),
// "Attempted to access deleted object IDs from a \(typeName(self)) outside its designated queue."
// )
// CoreStore.assert(
// !self.isCommitted,
// "Attempted to access deleted object IDs from an already committed \(typeName(self))."
// )
//
// return Set(self.context.deletedObjects.flatMap { $0 as? T }.map { $0.objectID })
// }
/**
Returns an editable proxy of the object with the specified `NSManagedObjectID`.
- parameter into: a `CSInto` clause specifying the entity type
- parameter objectID: the `NSManagedObjectID` for the object to be edited
- returns: an editable proxy for the specified `NSManagedObject`.
*/
@objc
@warn_unused_result
public func editInto(into: CSInto, objectID: NSManagedObjectID) -> NSManagedObject? {
return self.swift.edit(into.swift, objectID)
}
/**
Deletes a specified `NSManagedObject`.
- parameter object: the `NSManagedObject` to be deleted
*/
@objc
public func deleteObject(object: NSManagedObject?) {
self.swift.delete(object)
}
/**
Deletes the specified `NSManagedObject`s.
- parameter objects: the `NSManagedObject`s to be deleted
*/
@objc
public func deleteObjects(objects: [NSManagedObject]) {
self.swift.delete(objects)
}
/**
Refreshes all registered objects `NSManagedObject`s in the transaction.
*/
@objc
public func refreshAllObjectsAsFaults() {
self.swift.refreshAllObjectsAsFaults()
}
// MARK: Inspecting Pending Objects
/**
Returns all pending `NSManagedObject`s that were inserted to the transaction. This method should not be called after the `commit()` method was called.
- returns: an `NSSet` of pending `NSManagedObject`s that were inserted to the transaction.
*/
@objc
@warn_unused_result
public func insertedObjects() -> Set<NSManagedObject> {
return self.swift.insertedObjects()
}
/**
Returns all pending `NSManagedObject`s of the specified type that were inserted to the transaction. This method should not be called after the `commit()` method was called.
- parameter entity: the `NSManagedObject` subclass to filter
- returns: an `NSSet` of pending `NSManagedObject`s of the specified type that were inserted to the transaction.
*/
@objc
@warn_unused_result
public func insertedObjectsOfType(entity: NSManagedObject.Type) -> Set<NSManagedObject> {
return self.swift.insertedObjects(entity)
}
/**
Returns all pending `NSManagedObjectID`s that were inserted to the transaction. This method should not be called after the `commit()` method was called.
- returns: an `NSSet` of pending `NSManagedObjectID`s that were inserted to the transaction.
*/
@objc
@warn_unused_result
public func insertedObjectIDs() -> Set<NSManagedObjectID> {
return self.swift.insertedObjectIDs()
}
/**
Returns all pending `NSManagedObjectID`s of the specified type that were inserted to the transaction. This method should not be called after the `commit()` method was called.
- parameter entity: the `NSManagedObject` subclass to filter
- returns: an `NSSet` of pending `NSManagedObjectID`s of the specified type that were inserted to the transaction.
*/
@objc
@warn_unused_result
public func insertedObjectIDsOfType(entity: NSManagedObject.Type) -> Set<NSManagedObjectID> {
return self.swift.insertedObjectIDs(entity)
}
/**
Returns all pending `NSManagedObject`s that were updated in the transaction. This method should not be called after the `commit()` method was called.
- returns: an `NSSet` of pending `NSManagedObject`s that were updated to the transaction.
*/
@objc
@warn_unused_result
public func updatedObjects() -> Set<NSManagedObject> {
return self.swift.updatedObjects()
}
/**
Returns all pending `NSManagedObject`s of the specified type that were updated in the transaction. This method should not be called after the `commit()` method was called.
- parameter entity: the `NSManagedObject` subclass to filter
- returns: an `NSSet` of pending `NSManagedObject`s of the specified type that were updated in the transaction.
*/
@objc
@warn_unused_result
public func updatedObjectsOfType(entity: NSManagedObject.Type) -> Set<NSManagedObject> {
return self.swift.updatedObjects(entity)
}
/**
Returns all pending `NSManagedObjectID`s that were updated in the transaction. This method should not be called after the `commit()` method was called.
- returns: an `NSSet` of pending `NSManagedObjectID`s that were updated in the transaction.
*/
@objc
@warn_unused_result
public func updatedObjectIDs() -> Set<NSManagedObjectID> {
return self.swift.updatedObjectIDs()
}
/**
Returns all pending `NSManagedObjectID`s of the specified type that were updated in the transaction. This method should not be called after the `commit()` method was called.
- parameter entity: the `NSManagedObject` subclass to filter
- returns: an `NSSet` of pending `NSManagedObjectID`s of the specified type that were updated in the transaction.
*/
@objc
@warn_unused_result
public func updatedObjectIDsOfType(entity: NSManagedObject.Type) -> Set<NSManagedObjectID> {
return self.swift.updatedObjectIDs(entity)
}
/**
Returns all pending `NSManagedObject`s that were deleted from the transaction. This method should not be called after the `commit()` method was called.
- returns: an `NSSet` of pending `NSManagedObject`s that were deleted from the transaction.
*/
@objc
@warn_unused_result
public func deletedObjects() -> Set<NSManagedObject> {
return self.swift.deletedObjects()
}
/**
Returns all pending `NSManagedObject`s of the specified type that were deleted from the transaction. This method should not be called after the `commit()` method was called.
- parameter entity: the `NSManagedObject` subclass to filter
- returns: an `NSSet` of pending `NSManagedObject`s of the specified type that were deleted from the transaction.
*/
@objc
@warn_unused_result
public func deletedObjectsOfType(entity: NSManagedObject.Type) -> Set<NSManagedObject> {
return self.swift.deletedObjects(entity)
}
/**
Returns all pending `NSManagedObjectID`s of the specified type that were deleted from the transaction. This method should not be called after the `commit()` method was called.
- parameter entity: the `NSManagedObject` subclass to filter
- returns: an `NSSet` of pending `NSManagedObjectID`s of the specified type that were deleted from the transaction.
*/
@objc
@warn_unused_result
public func deletedObjectIDs() -> Set<NSManagedObjectID> {
return self.swift.deletedObjectIDs()
}
/**
Returns all pending `NSManagedObjectID`s of the specified type that were deleted from the transaction. This method should not be called after the `commit()` method was called.
- parameter entity: the `NSManagedObject` subclass to filter
- returns: a `Set` of pending `NSManagedObjectID`s of the specified type that were deleted from the transaction.
*/
@objc
@warn_unused_result
public func deletedObjectIDsOfType(entity: NSManagedObject.Type) -> Set<NSManagedObjectID> {
return self.swift.deletedObjectIDs(entity)
}
// MARK: NSObject
@@ -420,11 +289,26 @@ public class CSBaseDataTransaction: NSObject {
// MARK: CoreStoreBridge
internal let swift: BaseDataTransaction
internal init(_ swiftObject: BaseDataTransaction) {
public required init(_ swiftObject: BaseDataTransaction) {
self.swift = swiftObject
self.swiftObject = swiftObject
super.init()
}
internal var swift: BaseDataTransaction {
return self.swiftObject
}
private let swiftObject: BaseDataTransaction
}
// MARK: - BaseDataTransaction
extension BaseDataTransaction: CoreStoreBridgeable {
// MARK: CoreStoreBridgeable
internal typealias ObjCType = CSBaseDataTransaction
}

View File

@@ -28,6 +28,17 @@ public extension CSCoreStore {
return CoreStore.defaultStack.entityTypesByName
}
/**
Returns the entity class for the given entity name from the `defaultStack`'s model.
- parameter name: the entity name
- returns: the `NSManagedObject` class for the given entity name, or `nil` if not found
*/
@objc
public static func entityClassWithName(name: String) -> NSManagedObject.Type? {
return CoreStore.defaultStack.entityTypesByName[name]
}
/**
Returns the `NSEntityDescription` for the specified `NSManagedObject` subclass from `defaultStack`'s model.
*/

View File

@@ -27,16 +27,6 @@ import Foundation
import CoreData
// MARK: - DataStack
extension DataStack: CoreStoreBridgeable {
// MARK: CoreStoreBridgeable
public typealias ObjCType = CSDataStack
}
// MARK: - CSDataStack
/**
@@ -144,6 +134,17 @@ public final class CSDataStack: NSObject, CoreStoreBridge {
return self.swift.entityTypesByName
}
/**
Returns the entity class for the given entity name from the stack's's model.
- parameter name: the entity name
- returns: the `NSManagedObject` class for the given entity name, or `nil` if not found
*/
@objc
public func entityClassWithName(name: String) -> NSManagedObject.Type? {
return self.swift.entityTypesByName[name]
}
/**
Returns the `NSEntityDescription` for the specified `NSManagedObject` subclass from stack's model.
*/
@@ -249,10 +250,20 @@ public final class CSDataStack: NSObject, CoreStoreBridge {
// MARK: CoreStoreBridge
public let swift: DataStack
internal let swift: DataStack
public required init(_ swiftObject: DataStack) {
internal init(_ swiftObject: DataStack) {
self.swift = swiftObject
}
}
// MARK: - DataStack
extension DataStack: CoreStoreBridgeable {
// MARK: CoreStoreBridgeable
internal typealias ObjCType = CSDataStack
}

View File

@@ -80,3 +80,114 @@ public enum CSErrorCode: Int {
*/
case InternalError
}
// MARK: Internal
internal extension ErrorType {
internal var swift: CoreStoreError {
if case let error as CoreStoreError = self {
return error
}
guard let error = (self as Any) as? NSError else {
return .Unknown
}
guard error.domain == CoreStoreErrorDomain else {
return .InternalError(NSError: error)
}
guard let code = CoreStoreErrorCode(rawValue: error.code) else {
return .Unknown
}
let info = error.userInfo
switch code {
case .UnknownError:
return .Unknown
case .DifferentPersistentStoreExistsAtURL:
guard case let existingPersistentStoreURL as NSURL = info["existingPersistentStoreURL"] else {
return .Unknown
}
return .DifferentStorageExistsAtURL(existingPersistentStoreURL: existingPersistentStoreURL)
case .MappingModelNotFound:
guard let localStoreURL = info["localStoreURL"] as? NSURL,
let targetModel = info["targetModel"] as? NSManagedObjectModel,
let targetModelVersion = info["targetModelVersion"] as? String else {
return .Unknown
}
return .MappingModelNotFound(localStoreURL: localStoreURL, targetModel: targetModel, targetModelVersion: targetModelVersion)
case .ProgressiveMigrationRequired:
guard let localStoreURL = info["localStoreURL"] as? NSURL else {
return .Unknown
}
return .ProgressiveMigrationRequired(localStoreURL: localStoreURL)
case .InternalError:
guard case let NSError as NSError = info["NSError"] else {
return .Unknown
}
return .InternalError(NSError: NSError)
}
}
internal var objc: NSError {
guard let error = self as? CoreStoreError else {
return ((self as Any) as? NSError) ?? self as NSError
}
let code: CoreStoreErrorCode
let info: [NSObject: AnyObject]
switch error {
case .Unknown:
code = .UnknownError
info = [:]
case .DifferentStorageExistsAtURL(let existingPersistentStoreURL):
code = .DifferentPersistentStoreExistsAtURL
info = [
"existingPersistentStoreURL": existingPersistentStoreURL
]
case .MappingModelNotFound(let localStoreURL, let targetModel, let targetModelVersion):
code = .MappingModelNotFound
info = [
"localStoreURL": localStoreURL,
"targetModel": targetModel,
"targetModelVersion": targetModelVersion
]
case .ProgressiveMigrationRequired(let localStoreURL):
code = .ProgressiveMigrationRequired
info = [
"localStoreURL": localStoreURL
]
case .InternalError(let NSError):
code = .InternalError
info = [
"NSError": NSError
]
}
return NSError(domain: CoreStoreErrorDomain, code: code.rawValue, userInfo: info)
}
}

View File

@@ -27,16 +27,6 @@ import Foundation
import CoreData
// MARK: - InMemoryStore
extension InMemoryStore: CoreStoreBridgeable {
// MARK: CoreStoreBridgeable
public typealias ObjCType = CSInMemoryStore
}
// MARK: - CSInMemoryStore
/**
@@ -112,10 +102,20 @@ public final class CSInMemoryStore: NSObject, CSStorageInterface, CoreStoreBridg
// MARK: CoreStoreBridge
public let swift: InMemoryStore
internal let swift: InMemoryStore
public required init(_ swiftObject: InMemoryStore) {
self.swift = swiftObject
}
}
// MARK: - InMemoryStore
extension InMemoryStore: CoreStoreBridgeable {
// MARK: CoreStoreBridgeable
internal typealias ObjCType = CSInMemoryStore
}

View File

@@ -0,0 +1,95 @@
//
// CSInto.swift
// CoreStore
//
// Created by John Estropia on 2016/03/24.
// Copyright © 2016 John Rommel Estropia. All rights reserved.
//
import UIKit
// MARK: - CSInto
/**
The `CSInto` serves as the Objective-C bridging type for `Into<T>`.
*/
@objc
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]]];
```
- parameter entityClass: the `NSManagedObject` class type to be created
- returns: a new `CSInto` with the specified entity class
*/
@objc
public static func entityClass(entityClass: AnyClass) -> CSInto {
return self.init(Into(entityClass))
}
/**
Initializes an `CSInto` clause with the specified configuration.
Sample Usage:
```
MyPersonEntity *person = [transaction create:[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
*/
@objc
public static func entityClass(entityClass: AnyClass, configuration: String?) -> CSInto {
return self.init(Into(entityClass, configuration))
}
// MARK: NSObject
public override var hash: Int {
return self.swift.hashValue
}
public override func isEqual(object: AnyObject?) -> Bool {
guard let object = object as? CSInto else {
return false
}
return self.swift == object.swift
}
// MARK: CoreStoreBridge
internal let swift: Into<NSManagedObject>
public required init<T: NSManagedObject>(_ swiftObject: Into<T>) {
self.swift = Into<NSManagedObject>(
entityClass: swiftObject.entityClass,
configuration: swiftObject.configuration,
inferStoreIfPossible: swiftObject.inferStoreIfPossible
)
super.init()
}
}
// MARK: - Into
extension Into: CoreStoreBridgeable {
// MARK: CoreStoreBridgeable
internal var objc: CSInto {
return CSInto(self)
}
}

View File

@@ -27,16 +27,6 @@ import Foundation
import CoreData
// MARK: - SQLiteStore
extension SQLiteStore: CoreStoreBridgeable {
// MARK: CoreStoreBridgeable
public typealias ObjCType = CSSQLiteStore
}
// MARK: - CSSQLiteStore
/**
@@ -189,10 +179,20 @@ public final class CSSQLiteStore: NSObject, CSLocalStorage, CoreStoreBridge {
// MARK: CoreStoreBridge
public let swift: SQLiteStore
internal let swift: SQLiteStore
public required init(_ swiftObject: SQLiteStore) {
self.swift = swiftObject
}
}
// MARK: - SQLiteStore
extension SQLiteStore: CoreStoreBridgeable {
// MARK: CoreStoreBridgeable
internal typealias ObjCType = CSSQLiteStore
}

View File

@@ -28,7 +28,7 @@ import Foundation
// MARK: - CoreStoreBridge
public protocol CoreStoreBridge: class, AnyObject {
internal protocol CoreStoreBridge: class, AnyObject {
associatedtype SwiftType
@@ -40,42 +40,18 @@ public protocol CoreStoreBridge: class, AnyObject {
// MARK: - CoreStoreBridgeable
public protocol CoreStoreBridgeable: _ObjectiveCBridgeable {
internal protocol CoreStoreBridgeable {
associatedtype ObjCType: CoreStoreBridge
var objc: ObjCType { get }
}
public extension CoreStoreBridgeable where Self == ObjCType.SwiftType {
static func _isBridgedToObjectiveC() -> Bool {
return true
}
static func _getObjectiveCType() -> Any.Type {
return ObjCType.self
}
func _bridgeToObjectiveC() -> ObjCType {
return ObjCType(self)
}
static func _forceBridgeFromObjectiveC(source: ObjCType, inout result: ObjCType.SwiftType?) {
result = source.swift
}
static func _conditionallyBridgeFromObjectiveC(source: ObjCType, inout result: ObjCType.SwiftType?) -> Bool {
result = source.swift
return true
}
internal extension CoreStoreBridgeable where Self == ObjCType.SwiftType {
var objc: ObjCType {
return self._bridgeToObjectiveC()
return ObjCType(self)
}
}