This is the sample project for the article:
Taking SwiftData further with Modern Concurrency
It demonstrates a possible architecture for using SwiftData in a production-level application, focusing on moving all database operations to a background thread to ensure a responsive UI, while adhering to modern Swift concurrency safety practices.
This project showcases a solution to the common problem of SwiftData's modelContext blocking the main thread. The key patterns used are:
-
Background Persistence with
@ModelActor: All CRUD (Create, Read, Update, Delete) operations are encapsulated withinRecordStore, an@ModelActor. This guarantees every database interaction happens on a dedicated background thread, preventing UI freezes. -
The "Protected Model" Pattern: To safely pass data between the main thread (SwiftUI Views) and the background
ModelActor, this project uses two model types:Record: The non-Sendable@Modelclass that lives exclusively within theRecordStore.ProtectedRecord: ASendablestructthat acts as a safe Data Transfer Object (DTO) for use across actor boundaries. This pattern is essential for Swift 6 concurrency safety.
-
MVVM Architecture: A separation of concerns between the View (
MainListView), ViewModel (MainListViewModel), and the persistence layer (RecordStore). -
Automatic UI Updates via
NotificationCenter: Instead of relying on the main-thread-bound@Queryproperty wrapper, theMainListViewModelobservesModelContext.didSavenotifications. When data is saved in the background, the ViewModel is notified and efficiently re-fetches the latest data, updating the UI automatically.
- Xcode 26 or later
- iOS 26 or later
- Clone the repository.
- Open
SwiftDataConcurrency.xcodeprojin Xcode. - Build and run the app on a simulator or device.
You can test the background processing by tapping the Add button in the toolbar. This will begin inserting a large number of records in the background without freezing the UI. The list will update automatically as batches are saved.
└── SwiftDataConcurrency/
├── SwiftDataConcurrencyApp.swift // App entry point, ModelContainer setup
├── Extensions/
│ └── DispatchQueue+Ext.swift // Debugging helper to check queue labels
├── Model/
│ ├── Record.swift // The @Model class (lives only in the store)
│ └── ProtectedRecord.swift // The Sendable struct (DTO for UI/actors)
├── Preview Data/
│ └── PreviewValues.swift // In-memory container for SwiftUI Previews
├── Store/
│ ├── RecordStore.swift // The @ModelActor implementation
│ └── Protocols/
│ ├── PersistenceStore.swift // Protocol defining the store's capabilities
│ ├── ProtectedModel.swift // Protocol for our Sendable model types
│ └── PersistenceError.swift // Error type for PersistenceStore
└── Views/
└── MainListView/
├── MainListView.swift // The SwiftUI View
└── MainListViewModel.swift// The ViewModel orchestrating UI and data
