Syncing SwiftData with server API

Hello,

I have a List view that populates from a REST API (listApiKeys() calls out to server). The response is decoded and stored in a swiftdata model. It looks like this:

NavigationStack(path: $path) {
            List {
                ForEach(keys) { key in
                    NavigationLink(value: key) {
                        Text(key.name)
                    }
                }
            }
            .navigationDestination(for: GtApiKey.self) { key in
                EditApiKeyView(apiKey: key, navigationPath: $path)
            }
            .refreshable {
                try? modelContext.delete(model: GtApiKey.self)
                await listApiKeys()
            }
            .toolbar {
                Button("Create Key", systemImage: "plus", action: createKey)
            }
        }
        .navigationTitle("test apikeys")
        .task {
            await listApiKeys()
        }

This all works fine and SwiftData stores everything great. If I click into a single object's edit view, everything works great as well. My question is how do I sync changes to the SwiftData entry back to the server. My edit view uses a @Bindable and any changes auto-sync to SwiftData (expected). But, I can't seem to figure out where to catch those events or prevent them before I can send the PUT call to the REST API.

This seems like a pretty basic pattern. But all the docs / tutorials load data then only change locally. I couldn't find one that get in sync w/ a server side API.

Thanks in advance!

Syncing SwiftData with server API
 
 
Q