diff --git a/CoreStore/Fetching and Querying/BaseDataTransaction+Querying.swift b/CoreStore/Fetching and Querying/BaseDataTransaction+Querying.swift index 861b805..4f2cd25 100644 --- a/CoreStore/Fetching and Querying/BaseDataTransaction+Querying.swift +++ b/CoreStore/Fetching and Querying/BaseDataTransaction+Querying.swift @@ -33,6 +33,84 @@ public extension BaseDataTransaction { // MARK: Public + /** + 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. + */ + public func fetchExisting(object: T) -> T? { + + do { + + return (try self.context.existingObjectWithID(object.objectID) as! T) + } + 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. + */ + public func fetchExisting(objectID: NSManagedObjectID) -> T? { + + do { + + return (try self.context.existingObjectWithID(objectID) as! T) + } + 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 + */ + public func fetchExisting(objects: [T]) -> [T] { + + var existingObjects = [T]() + for object in objects { + + do { + + let existingObject = try self.context.existingObjectWithID(object.objectID) as! T + existingObjects.append(existingObject) + } + catch _ { } + } + return existingObjects + } + + /** + 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 + */ + public func fetchExisting(objectIDs: [NSManagedObjectID]) -> [T] { + + var existingObjects = [T]() + for objectID in objectIDs { + + do { + + let existingObject = try self.context.existingObjectWithID(objectID) as! T + existingObjects.append(existingObject) + } + catch _ { } + } + return existingObjects + } + /** Fetches the first `NSManagedObject` instance that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.