diff --git a/CoreStore/Fetching and Querying/CoreStore+Querying.swift b/CoreStore/Fetching and Querying/CoreStore+Querying.swift index 1d28e95..c44db1b 100644 --- a/CoreStore/Fetching and Querying/CoreStore+Querying.swift +++ b/CoreStore/Fetching and Querying/CoreStore+Querying.swift @@ -31,6 +31,50 @@ public extension CoreStore { // MARK: Public + /** + Using the `defaultStack`, fetches the `NSManagedObject` instance in the `DataStack`'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 `DataStack` + - returns: the `NSManagedObject` instance if the object exists in the `DataStack`, or `nil` if not found. + */ + public static func fetchExisting(object: T) -> T? { + + return self.defaultStack.fetchExisting(object) + } + + /** + Using the `defaultStack`, fetches the `NSManagedObject` instance in the `DataStack`'s context from an `NSManagedObjectID`. + + - parameter objectID: the `NSManagedObjectID` for the object + - returns: the `NSManagedObject` instance if the object exists in the `DataStack`, or `nil` if not found. + */ + public static func fetchExisting(objectID: NSManagedObjectID) -> T? { + + return self.defaultStack.fetchExisting(objectID) + } + + /** + Using the `defaultStack`, fetches the `NSManagedObject` instances in the `DataStack`'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 `DataStack` + - returns: the `NSManagedObject` array for objects that exists in the `DataStack` + */ + public static func fetchExisting(objects: [T]) -> [T] { + + return self.defaultStack.fetchExisting(objects) + } + + /** + Using the `defaultStack`, fetches the `NSManagedObject` instances in the `DataStack`'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 `DataStack` + */ + public static func fetchExisting(objectIDs: [NSManagedObjectID]) -> [T] { + + return self.defaultStack.fetchExisting(objectIDs) + } + /** Using the `defaultStack`, fetches the first `NSManagedObject` instance that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses. diff --git a/CoreStore/Fetching and Querying/DataStack+Querying.swift b/CoreStore/Fetching and Querying/DataStack+Querying.swift index cdf57a9..cdec5fd 100644 --- a/CoreStore/Fetching and Querying/DataStack+Querying.swift +++ b/CoreStore/Fetching and Querying/DataStack+Querying.swift @@ -34,6 +34,84 @@ public extension DataStack { // MARK: Public + /** + Fetches the `NSManagedObject` instance in the `DataStack`'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 `DataStack` + - returns: the `NSManagedObject` instance if the object exists in the `DataStack`, or `nil` if not found. + */ + public func fetchExisting(object: T) -> T? { + + do { + + return (try self.mainContext.existingObjectWithID(object.objectID) as! T) + } + catch _ { + + return nil + } + } + + /** + Fetches the `NSManagedObject` instance in the `DataStack`'s context from an `NSManagedObjectID`. + + - parameter objectID: the `NSManagedObjectID` for the object + - returns: the `NSManagedObject` instance if the object exists in the `DataStack`, or `nil` if not found. + */ + public func fetchExisting(objectID: NSManagedObjectID) -> T? { + + do { + + return (try self.mainContext.existingObjectWithID(objectID) as! T) + } + catch _ { + + return nil + } + } + + /** + Fetches the `NSManagedObject` instances in the `DataStack`'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 `DataStack` + - returns: the `NSManagedObject` array for objects that exists in the `DataStack` + */ + public func fetchExisting(objects: [T]) -> [T] { + + var existingObjects = [T]() + for object in objects { + + do { + + let existingObject = try self.mainContext.existingObjectWithID(object.objectID) as! T + existingObjects.append(existingObject) + } + catch _ { } + } + return existingObjects + } + + /** + Fetches the `NSManagedObject` instances in the `DataStack`'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 `DataStack` + */ + public func fetchExisting(objectIDs: [NSManagedObjectID]) -> [T] { + + var existingObjects = [T]() + for objectID in objectIDs { + + do { + + let existingObject = try self.mainContext.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.