# FetchableSource

    public protocol FetchableSource : AnyObject

Encapsulates containers which manages an internal `NSManagedObjectContext`, such as `DataStack`s and transactions, that can be used for fetching objects. CoreStore provides implementations for this protocol and should be used as a read-only abstraction.

- `fetchExisting(_:)`

  Fetches the `DynamicObject` instance in the `FetchableSource`‘s context from a reference created from another managed object context.

  #### Declaration

  Swift

      func fetchExisting<D>(_ object: D) -> D? where D : DynamicObject

  #### Parameters

  `object` | a reference to the object created/fetched outside the `FetchableSource`‘s context

  #### Return Value

  the `DynamicObject` instance if the object exists in the `FetchableSource`‘s context, or `nil` if not found.

- `fetchExisting(_:)`

  Fetches the `DynamicObject` instance in the `FetchableSource`‘s context from an `NSManagedObjectID`.

  #### Declaration

  Swift

      func fetchExisting<D>(_ objectID: NSManagedObjectID) -> D? where D : DynamicObject

  #### Parameters

  `objectID` | the `NSManagedObjectID` for the object

  #### Return Value

  the `DynamicObject` instance if the object exists in the `FetchableSource`, or `nil` if not found.

- `fetchExisting(_:)`

  Fetches the `DynamicObject` instances in the `FetchableSource`‘s context from references created from another managed object context.

  #### Declaration

  Swift

      func fetchExisting<D, S>(_ objects: S) -> [D] where D : DynamicObject, D == S.Element, S : Sequence

  #### Parameters

  `objects` | an array of `DynamicObject`s created/fetched outside the `FetchableSource`‘s context

  #### Return Value

  the `DynamicObject` array for objects that exists in the `FetchableSource`

- `fetchExisting(_:)`

  Fetches the `DynamicObject` instances in the `FetchableSource`‘s context from a list of `NSManagedObjectID`.

  #### Declaration

  Swift

      func fetchExisting<D, S>(_ objectIDs: S) -> [D] where D : DynamicObject, S : Sequence, S.Element == NSManagedObjectID

  #### Parameters

  `objectIDs` | the `NSManagedObjectID` array for the objects

  #### Return Value

  the `DynamicObject` array for objects that exists in the `FetchableSource`‘s context

- `fetchOne(_:_:)`

  Fetches the first `DynamicObject` instance that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Declaration

  Swift

      func fetchOne<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> D? where D : DynamicObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `fetchClauses` | a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Return Value

  the first `DynamicObject` instance that satisfies the specified `FetchClause`s

- `fetchOne(_:_:)`

  Fetches the first `DynamicObject` instance that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Declaration

  Swift

      func fetchOne<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> D? where D : DynamicObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `fetchClauses` | a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Return Value

  the first `DynamicObject` instance that satisfies the specified `FetchClause`s

- `fetchOne(_:)`

  Fetches the first `DynamicObject` instance that satisfies the specified `FetchChainableBuilderType` built from a chain of clauses.

      let youngestTeen = source.fetchOne(
          From<MyPersonEntity>()
              .where(\.age > 18)
              .orderBy(.ascending(\.age))
      )

  #### Declaration

  Swift

      func fetchOne<B>(_ clauseChain: B) -> B.ObjectType? where B : FetchChainableBuilderType

  #### Parameters

  `clauseChain` | a `FetchChainableBuilderType` built from a chain of clauses

  #### Return Value

  the first `DynamicObject` instance that satisfies the specified `FetchChainableBuilderType`

- `fetchAll(_:_:)`

  Fetches all `DynamicObject` instances that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Declaration

  Swift

      func fetchAll<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> [D]? where D : DynamicObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `fetchClauses` | a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Return Value

  all `DynamicObject` instances that satisfy the specified `FetchClause`s

- `fetchAll(_:_:)`

  Fetches all `DynamicObject` instances that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Declaration

  Swift

      func fetchAll<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> [D]? where D : DynamicObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `fetchClauses` | a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Return Value

  all `DynamicObject` instances that satisfy the specified `FetchClause`s

- `fetchAll(_:)`

  Fetches all `DynamicObject` instances that satisfy the specified `FetchChainableBuilderType` built from a chain of clauses.

      let people = source.fetchAll(
          From<MyPersonEntity>()
              .where(\.age > 18)
              .orderBy(.ascending(\.age))
      )

  #### Declaration

  Swift

      func fetchAll<B>(_ clauseChain: B) -> [B.ObjectType]? where B : FetchChainableBuilderType

  #### Parameters

  `clauseChain` | a `FetchChainableBuilderType` built from a chain of clauses

  #### Return Value

  all `DynamicObject` instances that satisfy the specified `FetchChainableBuilderType`

- `fetchCount(_:_:)`

  Fetches the number of `DynamicObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Declaration

  Swift

      func fetchCount<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> Int? where D : DynamicObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `fetchClauses` | a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Return Value

  the number `DynamicObject`s that satisfy the specified `FetchClause`s

- `fetchCount(_:_:)`

  Fetches the number of `DynamicObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Declaration

  Swift

      func fetchCount<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> Int? where D : DynamicObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `fetchClauses` | a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Return Value

  the number `DynamicObject`s that satisfy the specified `FetchClause`s

- `fetchCount(_:)`

  Fetches the number of `DynamicObject`s that satisfy the specified `FetchChainableBuilderType` built from a chain of clauses.

      let numberOfAdults = source.fetchCount(
          From<MyPersonEntity>()
              .where(\.age > 18)
              .orderBy(.ascending(\.age))
      )

  #### Declaration

  Swift

      func fetchCount<B>(_ clauseChain: B) -> Int? where B : FetchChainableBuilderType

  #### Parameters

  `clauseChain` | a `FetchChainableBuilderType` built from a chain of clauses

  #### Return Value

  the number `DynamicObject`s that satisfy the specified `FetchChainableBuilderType`

- `fetchObjectID(_:_:)`

  Fetches the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Declaration

  Swift

      func fetchObjectID<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> NSManagedObjectID? where D : DynamicObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `fetchClauses` | a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Return Value

  the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s

- `fetchObjectID(_:_:)`

  Fetches the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Declaration

  Swift

      func fetchObjectID<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> NSManagedObjectID? where D : DynamicObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `fetchClauses` | a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Return Value

  the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchClause`s

- `fetchObjectID(_:)`

  Fetches the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchChainableBuilderType` built from a chain of clauses.

      let youngestTeenID = source.fetchObjectID(
          From<MyPersonEntity>()
              .where(\.age > 18)
              .orderBy(.ascending(\.age))
      )

  #### Declaration

  Swift

      func fetchObjectID<B>(_ clauseChain: B) -> NSManagedObjectID? where B : FetchChainableBuilderType

  #### Parameters

  `clauseChain` | a `FetchChainableBuilderType` built from a chain of clauses

  #### Return Value

  the `NSManagedObjectID` for the first `DynamicObject` that satisfies the specified `FetchChainableBuilderType`

- `fetchObjectIDs(_:_:)`

  Fetches the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Declaration

  Swift

      func fetchObjectIDs<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> [NSManagedObjectID]? where D : DynamicObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `fetchClauses` | a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Return Value

  the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s

- `fetchObjectIDs(_:_:)`

  Fetches the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Declaration

  Swift

      func fetchObjectIDs<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> [NSManagedObjectID]? where D : DynamicObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `fetchClauses` | a series of `FetchClause` instances for the fetch request. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Return Value

  the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchClause`s

- `fetchObjectIDs(_:)`

  Fetches the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchChainableBuilderType` built from a chain of clauses.

      let idsOfAdults = source.fetchObjectIDs(
          From<MyPersonEntity>()
              .where(\.age > 18)
              .orderBy(.ascending(\.age))
      )

  #### Declaration

  Swift

      func fetchObjectIDs<B>(_ clauseChain: B) -> [NSManagedObjectID]? where B : FetchChainableBuilderType

  #### Parameters

  `clauseChain` | a `FetchChainableBuilderType` built from a chain of clauses

  #### Return Value

  the `NSManagedObjectID` for all `DynamicObject`s that satisfy the specified `FetchChainableBuilderType`

- `unsafeContext()`

  The internal `NSManagedObjectContext` managed by this `FetchableSource`. Using this context directly should typically be avoided, and is provided by CoreStore only for extremely specialized cases.

  #### Declaration

  Swift

      func unsafeContext() -> NSManagedObjectContext