updated README, added more utilities for updating and deleting objects from transactions

This commit is contained in:
John Rommel Estropia
2015-05-30 17:26:06 +09:00
parent 6b8d5df049
commit 869660d41d
9 changed files with 257 additions and 11 deletions

View File

@@ -109,11 +109,25 @@ public final class AsynchronousDataTransaction: BaseDataTransaction {
:param: object the `NSManagedObject` type to be edited
:returns: an editable proxy for the specified `NSManagedObject`.
*/
public override func fetch<T: NSManagedObject>(object: T?) -> T? {
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.fetch(object)
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.
:param: into an `Into` clause specifying the entity type
:param: objectID the `NSManagedObjectID` for the object to be edited
:returns: an editable proxy for the specified `NSManagedObject`.
*/
public override func edit<T: NSManagedObject>(into: Into<T>, _ objectID: NSManagedObjectID) -> T? {
CoreStore.assert(!self.isCommitted, "Attempted to update an entity of type <\(T.self)> from an already committed \(typeName(self)).")
return super.edit(into, objectID)
}
/**
@@ -128,6 +142,31 @@ public final class AsynchronousDataTransaction: BaseDataTransaction {
super.delete(object)
}
/**
Deletes the specified `NSManagedObject`'s.
:param: object the `NSManagedObject` type to be deleted
:param: objects other `NSManagedObject`'s type to be deleted
*/
public override func delete(object: NSManagedObject?, _ objects: NSManagedObject?...) {
CoreStore.assert(!self.isCommitted, "Attempted to delete an entities from an already committed \(typeName(self)).")
super.delete([object] + objects)
}
/**
Deletes the specified `NSManagedObject`'s.
:param: objects the `NSManagedObject`'s type to be deleted
*/
public override func delete(objects: [NSManagedObject?]) {
CoreStore.assert(!self.isCommitted, "Attempted to delete an entities from an already committed \(typeName(self)).")
super.delete(objects)
}
/**
Rolls back the transaction by resetting the `NSManagedObjectContext`. After calling this method, all `NSManagedObjects` fetched within the transaction will become invalid. This method should not be used after the `commit()` method was already called once.
*/

View File

@@ -45,6 +45,11 @@ public struct Into<T: NSManagedObject> {
// MARK: Public
internal static var defaultConfigurationName: String {
return "PF_DEFAULT_CONFIGURATION_NAME"
}
/**
Initializes an `Into` clause.
Sample Usage:
@@ -180,13 +185,28 @@ public /*abstract*/ class BaseDataTransaction {
:param: object the `NSManagedObject` type to be edited
:returns: an editable proxy for the specified `NSManagedObject`.
*/
public func fetch<T: NSManagedObject>(object: T?) -> T? {
public func edit<T: NSManagedObject>(object: T?) -> T? {
CoreStore.assert(self.transactionQueue.isCurrentExecutionContext(), "Attempted to update an entity of type \(typeName(object)) outside its designated queue.")
return object?.inContext(self.context)
}
/**
Returns an editable proxy of the object with the specified `NSManagedObjectID`.
:param: into an `Into` clause specifying the entity type
:param: objectID the `NSManagedObjectID` for the object to be edited
:returns: an editable proxy for the specified `NSManagedObject`.
*/
public func edit<T: NSManagedObject>(into: Into<T>, _ objectID: NSManagedObjectID) -> T? {
CoreStore.assert(self.transactionQueue.isCurrentExecutionContext(), "Attempted to update an entity of type <\(T.self)> outside its designated queue.")
CoreStore.assert(into.inferStoreIfPossible || (into.configuration ?? Into.defaultConfigurationName) == objectID.persistentStore?.configurationName, "Attempted to update an entity of type <\(T.self)> but the specified persistent store do not match the `NSManagedObjectID`.")
return T.inContext(self.context, withObjectID: objectID)
}
/**
Deletes a specified `NSManagedObject`.
@@ -194,11 +214,38 @@ public /*abstract*/ class BaseDataTransaction {
*/
public func delete(object: NSManagedObject?) {
CoreStore.assert(self.transactionQueue.isCurrentExecutionContext(), "Attempted to delete an entity of type \(typeName(object)) outside its designated queue.")
CoreStore.assert(self.transactionQueue.isCurrentExecutionContext(), "Attempted to delete an entity outside its designated queue.")
object?.inContext(self.context)?.deleteFromContext()
}
/**
Deletes the specified `NSManagedObject`'s.
:param: object the `NSManagedObject` type to be deleted
:param: objects other `NSManagedObject`'s type to be deleted
*/
public func delete(object: NSManagedObject?, _ objects: NSManagedObject?...) {
self.delete([object] + objects)
}
/**
Deletes the specified `NSManagedObject`'s.
:param: objects the `NSManagedObject`'s type to be deleted
*/
public func delete(objects: [NSManagedObject?]) {
CoreStore.assert(self.transactionQueue.isCurrentExecutionContext(), "Attempted to delete entities outside their designated queue.")
let context = self.context
for object in objects {
object?.inContext(context)?.deleteFromContext()
}
}
// MARK: Saving changes
/**

View File

@@ -88,11 +88,25 @@ public final class SynchronousDataTransaction: BaseDataTransaction {
:param: object the `NSManagedObject` type to be edited
:returns: an editable proxy for the specified `NSManagedObject`.
*/
public override func fetch<T: NSManagedObject>(object: T?) -> T? {
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.fetch(object)
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.
:param: into an `Into` clause specifying the entity type
:param: objectID the `NSManagedObjectID` for the object to be edited
:returns: an editable proxy for the specified `NSManagedObject`.
*/
public override func edit<T: NSManagedObject>(into: Into<T>, _ objectID: NSManagedObjectID) -> T? {
CoreStore.assert(!self.isCommitted, "Attempted to update an entity of type <\(T.self)> from an already committed \(typeName(self)).")
return super.edit(into, objectID)
}
/**
@@ -107,6 +121,31 @@ public final class SynchronousDataTransaction: BaseDataTransaction {
super.delete(object)
}
/**
Deletes the specified `NSManagedObject`'s.
:param: object the `NSManagedObject` type to be deleted
:param: objects other `NSManagedObject`'s type to be deleted
*/
public override func delete(object: NSManagedObject?, _ objects: NSManagedObject?...) {
CoreStore.assert(!self.isCommitted, "Attempted to delete an entities from an already committed \(typeName(self)).")
super.delete([object] + objects)
}
/**
Deletes the specified `NSManagedObject`'s.
:param: objects the `NSManagedObject`'s type to be deleted
*/
public override func delete(objects: [NSManagedObject?]) {
CoreStore.assert(!self.isCommitted, "Attempted to delete an entities from an already committed \(typeName(self)).")
super.delete(objects)
}
/**
Rolls back the transaction by resetting the `NSManagedObjectContext`. After calling this method, all `NSManagedObjects` fetched within the transaction will become invalid. This method should not be used after the `commit()` method was already called once.
*/