# UnsafeDataTransaction Class Reference

# UnsafeDataTransaction

    public final class UnsafeDataTransaction : BaseDataTransaction

The `UnsafeDataTransaction` provides an interface for non-contiguous `NSManagedObject` or `CoreStoreObject` creates, updates, and deletes. This is useful for making temporary changes, such as partially filled forms. An unsafe transaction object should typically be only used from the main queue.

- `commit(_:)`

  Saves the transaction changes asynchronously. For an `UnsafeDataTransaction`, multiple commits are allowed, although it is the developer’s responsibility to ensure a reasonable leeway to prevent blocking the main thread.

  #### Declaration

  Swift

      public func commit(_ completion: @escaping (_ error: CoreStoreError?) -> Void)

  #### Parameters

  `completion` | the block executed after the save completes. Success or failure is reported by the optional `error` argument of the block.

- `commitAndWait()`

  Saves the transaction changes and waits for completion synchronously. For an `UnsafeDataTransaction`, multiple commits are allowed, although it is the developer’s responsibility to ensure a reasonable leeway to prevent blocking the main thread.

  Throws

  a `CoreStoreError` value indicating the failure.

  #### Declaration

  Swift

      public func commitAndWait() throws

- `rollback()`

  Rolls back the transaction.

  #### Declaration

  Swift

      public func rollback()

- `undo()`

  Undo’s the last change made to the transaction.

  #### Declaration

  Swift

      public func undo()

- `flush()`

  Immediately flushes all pending changes to the transaction’s observers. This is useful in conjunction with `ListMonitor`s and `ObjectMonitor`s created from `UnsafeDataTransaction`s used to manage temporary scratch data.

  Important

  Note that unlike `commit()`, `flush()` does not propagate/save updates to the `DataStack` and the persistent store. However, the flushed changes will be seen by children transactions created further from the current transaction (i.e. through `transaction.beginUnsafe()`)

  Throws

  an error thrown from `closure`, or an error thrown by Core Data (usually validation errors or conflict errors)

  #### Declaration

  Swift

      public func flush()

- `flush(closure:)`

  Flushes all pending changes to the transaction’s observers at the end of the `closure`‘s execution. This is useful in conjunction with `ListMonitor`s and `ObjectMonitor`s created from `UnsafeDataTransaction`s used to manage temporary scratch data.

  Important

  Note that unlike `commit()`, `flush()` does not propagate/save updates to the `DataStack` and the persistent store. However, the flushed changes will be seen by children transactions created further from the current transaction (i.e. through `transaction.beginUnsafe()`)

  Throws

  an error thrown from `closure`, or an error thrown by Core Data (usually validation errors or conflict errors)

  #### Declaration

  Swift

      public func flush(closure: () throws -> Void) rethrows

  #### Parameters

  `closure` | the closure where changes can be made prior to the flush

- `redo()`

  Redo’s the last undone change to the transaction.

  #### Declaration

  Swift

      public func redo()

- `beginUnsafe(supportsUndo:)`

  Begins a child transaction where `NSManagedObject` or `CoreStoreObject` creates, updates, and deletes can be made. This is useful for making temporary changes, such as partially filled forms.

  - prameter supportsUndo: `undo()`, `redo()`, and `rollback()` methods are only available when this parameter is `true`, otherwise those method will raise an exception. Defaults to `false`. Note that turning on Undo support may heavily impact performance especially on iOS or watchOS where memory is limited.

  #### Declaration

  Swift

      public func beginUnsafe(supportsUndo: Bool = false) -> UnsafeDataTransaction

  #### Return Value

  an `UnsafeDataTransaction` instance where creates, updates, and deletes can be made.

### CoreStoreSwiftType

- `bridgeToObjectiveC`

  #### Declaration

  Swift

      public var bridgeToObjectiveC: CSUnsafeDataTransaction { get }

### CustomDebugStringConvertible

- `debugDescription`

  #### Declaration

  Swift

      public var debugDescription: String { get }

### UnsafeDataTransaction

- `createFetchedResultsController(_:_:_:)`

  Utility for creating an `NSFetchedResultsController` from the `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.

  Note

  It is the caller’s responsibility to call `performFetch()` on the created `NSFetchedResultsController`.

  #### Declaration

  Swift

      @nonobjc
      public func createFetchedResultsController<D>(_ from: From<D>, _ sectionBy: SectionBy<D>, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<D> where D : NSManagedObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `sectionBy` | a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections
  `fetchClauses` | a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Return Value

  an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`

- `createFetchedResultsController(_:_:_:)`

  Utility for creating an `NSFetchedResultsController` from the `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.

  Note

  It is the caller’s responsibility to call `performFetch()` on the created `NSFetchedResultsController`.

  #### Declaration

  Swift

      @nonobjc
      public func createFetchedResultsController<D>(_ from: From<D>, _ sectionBy: SectionBy<D>, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<D> where D : NSManagedObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `sectionBy` | a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections
  `fetchClauses` | a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Return Value

  an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`

- `createFetchedResultsController(_:_:)`

  Utility for creating an `NSFetchedResultsController` from the `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.

  Note

  It is the caller’s responsibility to call `performFetch()` on the created `NSFetchedResultsController`.

  #### Declaration

  Swift

      @nonobjc
      public func createFetchedResultsController<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<D> where D : NSManagedObject

  #### Parameters

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

  #### Return Value

  an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`

- `createFetchedResultsController(_:_:)`

  Utility for creating an `NSFetchedResultsController` from the `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.

  Note

  It is the caller’s responsibility to call `performFetch()` on the created `NSFetchedResultsController`.

  #### Declaration

  Swift

      @nonobjc
      public func createFetchedResultsController<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<D> where D : NSManagedObject

  #### Parameters

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

  #### Return Value

  an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`

### UnsafeDataTransaction

- `monitorObject(_:)`

  Creates a `ObjectMonitor` for the specified `DynamicObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `DynamicObject`.

  #### Declaration

  Swift

      public func monitorObject<D>(_ object: D) -> ObjectMonitor<D> where D : DynamicObject

  #### Parameters

  `object` | the `DynamicObject` to observe changes from

  #### Return Value

  a `ObjectMonitor` that monitors changes to `object`

- `monitorList(_:_:)`

  Creates a `ListMonitor` for a list of `DynamicObject`s that satisfy the specified fetch clauses. Multiple `ListObserver`s may then register themselves to be notified when changes are made to the list.

  #### Declaration

  Swift

      public func monitorList<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> ListMonitor<D> where D : DynamicObject

  #### Parameters

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

  #### Return Value

  a `ListMonitor` instance that monitors changes to the list

- `monitorList(_:_:)`

  Creates a `ListMonitor` for a list of `DynamicObject`s that satisfy the specified fetch clauses. Multiple `ListObserver`s may then register themselves to be notified when changes are made to the list.

  #### Declaration

  Swift

      public func monitorList<D>(_ from: From<D>, _ fetchClauses: [FetchClause]) -> ListMonitor<D> where D : DynamicObject

  #### Parameters

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

  #### Return Value

  a `ListMonitor` instance that monitors changes to the list

- `monitorList(_:)`

  Asynchronously creates a `ListMonitor` for a list of `DynamicObject`s that satisfy the specified `FetchChainableBuilderType` built from a chain of clauses. Since `NSFetchedResultsController` greedily locks the persistent store on initial fetch, you may prefer this method instead of the synchronous counterpart to avoid deadlocks while background updates/saves are being executed.

      transaction.monitorList(
          { (monitor) in
              self.monitor = monitor
          },
          From<MyPersonEntity>()
              .where(\.age > 18)
              .orderBy(.ascending(\.age))
      )

  #### Declaration

  Swift

      public func monitorList<B>(_ clauseChain: B) -> ListMonitor<B.ObjectType> where B : FetchChainableBuilderType

  #### Parameters

  `createAsynchronously` | the closure that receives the created `ListMonitor` instance
  `clauseChain` | a `FetchChainableBuilderType` built from a chain of clauses

  #### Return Value

  a `ListMonitor` for a list of `DynamicObject`s that satisfy the specified `FetchChainableBuilderType`

- `monitorList(createAsynchronously:_:_:)`

  Asynchronously creates a `ListMonitor` for a list of `DynamicObject`s that satisfy the specified fetch clauses. Multiple `ListObserver`s may then register themselves to be notified when changes are made to the list. Since `NSFetchedResultsController` greedily locks the persistent store on initial fetch, you may prefer this method instead of the synchronous counterpart to avoid deadlocks while background updates/saves are being executed.

  #### Declaration

  Swift

      public func monitorList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ fetchClauses: FetchClause...)

  #### Parameters

  `createAsynchronously` | the closure that receives the created `ListMonitor` instance
  `from` | a `From` clause indicating the entity type
  `fetchClauses` | a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

- `monitorList(createAsynchronously:_:_:)`

  Asynchronously creates a `ListMonitor` for a list of `DynamicObject`s that satisfy the specified fetch clauses. Multiple `ListObserver`s may then register themselves to be notified when changes are made to the list. Since `NSFetchedResultsController` greedily locks the persistent store on initial fetch, you may prefer this method instead of the synchronous counterpart to avoid deadlocks while background updates/saves are being executed.

  #### Declaration

  Swift

      public func monitorList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ fetchClauses: [FetchClause])

  #### Parameters

  `createAsynchronously` | the closure that receives the created `ListMonitor` instance
  `from` | a `From` clause indicating the entity type
  `fetchClauses` | a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

- `monitorList(createAsynchronously:_:)`

  Asynchronously creates a `ListMonitor` for a list of `DynamicObject`s that satisfy the specified `FetchChainableBuilderType` built from a chain of clauses. Since `NSFetchedResultsController` greedily locks the persistent store on initial fetch, you may prefer this method instead of the synchronous counterpart to avoid deadlocks while background updates/saves are being executed.

      dataStack.monitorList(
          createAsynchronously: { (monitor) in
              self.monitor = monitor
          },
          From<MyPersonEntity>()
              .where(\.age > 18)
              .orderBy(.ascending(\.age))
      )

  #### Declaration

  Swift

      public func monitorList<B: FetchChainableBuilderType>(createAsynchronously: @escaping (ListMonitor<B.ObjectType>) -> Void, _ clauseChain: B)

  #### Parameters

  `createAsynchronously` | the closure that receives the created `ListMonitor` instance
  `clauseChain` | a `FetchChainableBuilderType` built from a chain of clauses

- `monitorSectionedList(_:_:_:)`

  Creates a `ListMonitor` for a sectioned list of `DynamicObject`s that satisfy the specified fetch clauses. Multiple `ListObserver`s may then register themselves to be notified when changes are made to the list.

  #### Declaration

  Swift

      public func monitorSectionedList<D>(_ from: From<D>, _ sectionBy: SectionBy<D>, _ fetchClauses: FetchClause...) -> ListMonitor<D> where D : DynamicObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `sectionBy` | a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
  `fetchClauses` | a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Return Value

  a `ListMonitor` instance that monitors changes to the list

- `monitorSectionedList(_:_:_:)`

  Creates a `ListMonitor` for a sectioned list of `DynamicObject`s that satisfy the specified fetch clauses. Multiple `ListObserver`s may then register themselves to be notified when changes are made to the list.

  #### Declaration

  Swift

      public func monitorSectionedList<D>(_ from: From<D>, _ sectionBy: SectionBy<D>, _ fetchClauses: [FetchClause]) -> ListMonitor<D> where D : DynamicObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `sectionBy` | a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
  `fetchClauses` | a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

  #### Return Value

  a `ListMonitor` instance that monitors changes to the list

- `monitorSectionedList(_:)`

  Creates a `ListMonitor` for a sectioned list of `DynamicObject`s that satisfy the specified `SectionMonitorBuilderType` built from a chain of clauses.

      let monitor = transaction.monitorSectionedList(
          From<MyPersonEntity>()
              .sectionBy(\.age, { "\($0!) years old" })
              .where(\.age > 18)
              .orderBy(.ascending(\.age))
      )

  #### Declaration

  Swift

      public func monitorSectionedList<B>(_ clauseChain: B) -> ListMonitor<B.ObjectType> where B : SectionMonitorBuilderType

  #### Parameters

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

  #### Return Value

  a `ListMonitor` for a list of `DynamicObject`s that satisfy the specified `SectionMonitorBuilderType`

- `monitorSectionedList(createAsynchronously:_:_:_:)`

  Asynchronously creates a `ListMonitor` for a sectioned list of `DynamicObject`s that satisfy the specified fetch clauses. Multiple `ListObserver`s may then register themselves to be notified when changes are made to the list. Since `NSFetchedResultsController` greedily locks the persistent store on initial fetch, you may prefer this method instead of the synchronous counterpart to avoid deadlocks while background updates/saves are being executed.

  #### Declaration

  Swift

      public func monitorSectionedList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ sectionBy: SectionBy<D>, _ fetchClauses: FetchClause...)

  #### Parameters

  `createAsynchronously` | the closure that receives the created `ListMonitor` instance
  `from` | a `From` clause indicating the entity type
  `sectionBy` | a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
  `fetchClauses` | a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

- `monitorSectionedList(createAsynchronously:_:_:_:)`

  Asynchronously creates a `ListMonitor` for a sectioned list of `DynamicObject`s that satisfy the specified fetch clauses. Multiple `ListObserver`s may then register themselves to be notified when changes are made to the list. Since `NSFetchedResultsController` greedily locks the persistent store on initial fetch, you may prefer this method instead of the synchronous counterpart to avoid deadlocks while background updates/saves are being executed.

  #### Declaration

  Swift

      public func monitorSectionedList<D>(createAsynchronously: @escaping (ListMonitor<D>) -> Void, _ from: From<D>, _ sectionBy: SectionBy<D>, _ fetchClauses: [FetchClause])

  #### Parameters

  `createAsynchronously` | the closure that receives the created `ListMonitor` instance
  `from` | a `From` clause indicating the entity type
  `sectionBy` | a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
  `fetchClauses` | a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.

- `monitorSectionedList(createAsynchronously:_:)`

  Asynchronously creates a `ListMonitor` for a sectioned list of `DynamicObject`s that satisfy the specified `SectionMonitorBuilderType` built from a chain of clauses.

      transaction.monitorSectionedList(
          { (monitor) in
              self.monitor = monitor
          },
          From<MyPersonEntity>()
              .sectionBy(\.age, { "\($0!) years old" })
              .where(\.age > 18)
              .orderBy(.ascending(\.age))
      )

  #### Declaration

  Swift

      public func monitorSectionedList<B: SectionMonitorBuilderType>(createAsynchronously: @escaping (ListMonitor<B.ObjectType>) -> Void, _ clauseChain: B)

  #### Parameters

  `createAsynchronously` | the closure that receives the created `ListMonitor` instance
  `clauseChain` | a `SectionMonitorBuilderType` built from a chain of clauses