2026 SwiftSwiftUI & UI FrameworksApp Services
WWDC26 · 13 min · Swift / SwiftUI & UI Frameworks / App Services
What’s new in SwiftData
Discover the latest enhancements to SwiftData. We’ll show you how to persist custom and third-party types using Codable, and group fetched data into sections in your SwiftUI app. We’ll also explore how to observe data store changes anywhere else using ResultsObserver and HistoryObserver, giving you the flexibility to drive powerful state objects and react precisely to model updates.
Watch at developer.apple.com ↗Chapters
Code shown on screen · 4 snippets
Sectioned fetching
// Sectioned fetching
struct TripListView: View {
(sort: \Trip.startDate,
sectionBy: \.destination)
var trips: [Trip]
var body: some View: {
List(selection: $selection) {
ForEach(_trips.sections) {section in
Section(section.id) {
ForEach(trips) {trip in
TripListItem(trip: trip)
}
}
}
}
}
} Using Codable
// Using Codable
import SwiftData
class Trip {
struct Location: Codable {
var latitude: Double
var longitude: Double
}
var name: String
var destination: String
var startDate: Date
var endDate: Date
var location: Location?
(.codable) var mapItemIdentifier: MKMapItem.Identifier?
} // Use observation to update map bounds
// Use observation to update map bounds
final class MapCameraController {
private let resultsObserver: ResultsObserver<Trip, Never>
var bounds: MapCameraBounds?
private var token: ObservationTracking.Token?
init(modelContext: ModelContext) throws {
resultsObserver = try ResultsObserver<Trip, Never>(modelContext: modelContext)
token = withContinuousObservation(options: [.didSet]) {[weak self], event in
self?.bounds = self?.calculateBounds(trips: resultsObserver.results)
}
}
private func calculateBounds(trips: [Trip]) -> MapCameraBounds? { / * */ }
} // Using HistoryObserver to sync with a server
// Using HistoryObserver to sync with a server
final class ServerSync {
private let observer: HistoryObserver
private var token: ObservationTracking.Token?
func start() throws {
self.observer = try HistoryObserver(authors: ["App"], modelContainer: modelContainer)
token = withContinuousObservation(options: .didSet) {[weak self] _ in
_ = self?.observer.eventCounter
self?.processChanges()
}
}
private func processChanges() {
// Fetch and process history transactions
}
} Resources
Related sessions
-
23 min -
17 min