-
Novedades de SwiftData
Descubre las últimas mejoras de SwiftData. Te mostraremos cómo conservar tipos personalizados y de terceros mediante Codable, y cómo agrupar los datos recuperados en secciones dentro de tu app desarrollada con SwiftUI. También veremos cómo observar los cambios en el almacén de datos desde cualquier otro lugar utilizando ModelResultsObserver y HistoryObserver, lo que te dará la flexibilidad necesaria para manejar potentes objetos de estado, integrar arquitecturas basadas en delegados y reaccionar con precisión a las actualizaciones del modelo.
Capítulos
- 0:00 - Introducción
- 0:53 - Secciona la obtención de datos
- 2:56 - Usa tipos personalizados
- 6:26 - Observa almacenes de datos con ResultsObserver
- 9:41 - Observa el historial con HistoryObserver
- 12:20 - Próximos pasos
Recursos
Videos relacionados
WWDC26
WWDC24
-
Buscar este video…
-
-
0:01 - Sectioned fetching
// Sectioned fetching struct TripListView: View { @Query(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) } } } } } } -
4:59 - Using Codable
// Using Codable import SwiftData @Model 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? @Attribute(.codable) var mapItemIdentifier: MKMapItem.Identifier? } -
8:20 - // Use observation to update map bounds
// Use observation to update map bounds @Observable @MainActor 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? { / * */ } } -
8:21 - // Using HistoryObserver to sync with a server
// Using HistoryObserver to sync with a server @SyncActor 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 } }
-