---
title: "transaction(block:)"
framework: swiftdata
role: symbol
role_heading: Instance Method
path: "swiftdata/modelcontext/transaction(block:)"
---

# transaction(block:)

Runs the provided closure, and once it finishes, writes any pending inserts, changes, and deletes to the persistent storage.

## Declaration

```swift
func transaction(block: () throws -> Void) throws
```

## Parameters

- `block`: The closure to run before performing a save operation.

## See Also

### Persisting unsaved changes

- [autosaveEnabled](swiftdata/modelcontext/autosaveenabled.md)
- [save()](swiftdata/modelcontext/save().md)
- [rollback()](swiftdata/modelcontext/rollback().md)
