# Swift Concurrency & Persistence (SwiftData, Core Data)

> Structured concurrency with async/await, tasks and actors, the MainActor, and persisting models with SwiftData or Core Data. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/mobile/ios-concurrency-data
Last updated: 2026-11-28

Structured concurrency with async/await, tasks and actors, the MainActor, and persisting models with SwiftData or Core Data.

Intermediate · about 10 hours · difficulty 4/5

Learn first: [SwiftUI Data Flow & Composition](https://www.internhack.in/roadmaps/tracks/mobile/swiftui-in-depth), [Async Code & the Main Thread](https://www.internhack.in/roadmaps/tracks/mobile/async-programming)

Modern iOS apps use Swift's built-in concurrency for networking and background work, and SwiftData (or Core Data in older code) for local persistence.

### Swift concurrency

- `async` functions and `await` calls; `URLSession.shared.data(from:)` is async.
- **Structured concurrency**: `async let` and `TaskGroup` run child tasks that are cancelled together.
- `Task { }` starts work from synchronous code; check `Task.isCancelled` in long loops.
- **Actors** protect mutable state from data races. `@MainActor` guarantees code runs on the main thread; view models that drive UI are usually `@MainActor`.
- Swift 6 strict concurrency checking turns many data races into compile errors. Expect `Sendable` warnings and learn to fix them rather than silence them.

### SwiftData

Mark classes with `@Model`, attach a `modelContainer` to your app, query with `@Query` in views, and insert/delete via `modelContext`. Plan schema migrations with versioned schemas.

### Core Data

Still common in existing apps: `NSManagedObjectContext`, fetch requests and background contexts. Learn to read it even if you start with SwiftData.

### Pitfalls

Updating UI state from a background task without `@MainActor` causes warnings or crashes.

**Good enough to move on:** your app loads data concurrently, caches it in SwiftData and shows cached content offline.

## Key concepts

- **async/await and URLSession**
- **async let and TaskGroup**
- **Task cancellation**
- **Actors and @MainActor**
- **Sendable and strict concurrency**
- **SwiftData models and queries**
- **Core Data basics**
- **Combine awareness**: Found in older codebases

## Mini project

Build a podcast browser that fetches several feeds in parallel with a TaskGroup and persists episodes in SwiftData for offline viewing.

## Check yourself

What does marking a view model with @MainActor guarantee, and why is that useful?

## Free resources

- [Swift concurrency](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/concurrency/) (Swift.org)
- [SwiftData](https://developer.apple.com/documentation/swiftdata) (Apple)
- [Core Data](https://developer.apple.com/documentation/coredata) (Apple)

Previous: [SwiftUI Data Flow & Composition](https://www.internhack.in/roadmaps/tracks/mobile/swiftui-in-depth) · Next: [HTTP & REST APIs for App Developers](https://www.internhack.in/roadmaps/tracks/mobile/http)

## More in iOS with Swift in Depth

- [iOS App Lifecycle, Xcode Projects & Capabilities](https://www.internhack.in/roadmaps/tracks/mobile/ios-fundamentals)
- [SwiftUI Data Flow & Composition](https://www.internhack.in/roadmaps/tracks/mobile/swiftui-in-depth)
- [Swift Concurrency & Persistence (SwiftData, Core Data)](https://www.internhack.in/roadmaps/tracks/mobile/ios-concurrency-data)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Mobile App Developer Roadmap](https://www.internhack.in/roadmaps/tracks/mobile)
