Very simple SwiftUI/SwiftData app below to demonstrate concepts. I've been using the same DetailView to handle add & update in SwiftData. Using modelConext.insert(person) seems to handle BOTH additions of a new record AND updating of the existing record without any problem. Is there any reason not to use this approach in a simple app like this? Minimalist UI below just to get to the modelContext.insert point:

import SwiftUI
import SwiftData
@Model
class Person {
var first: String
var last: String
init(first: String = "", last: String = "") {
self.first = first
self.last = last
}
}
struct DetailView: View {
@State var person: Person
@State private var first = ""
@State private var last = ""
@Environment(\.dismiss) private var dismiss
@Environment(\.modelContext) var modelContext
var body: some View {
VStack {
TextField("first", text: $first)
TextField("last", text: $last)
HStack {
Button("Cancel") {
dismiss()
}
Button("Save") {
person.first = first
person.last = last
modelContext.insert(person)
dismiss()
}
}
Spacer()
}
.onAppear() {
first = person.first
last = person.last
}
.padding()
.textFieldStyle(.roundedBorder)
}
}
struct ContentView: View {
@Query var people: [Person]
@State private var sheetIsPresented = false
@Environment(\.modelContext) var modelContext
var body: some View {
NavigationStack {
Button("Add") {
print("Link clicked!")
sheetIsPresented.toggle()
}
List {
ForEach(people) { person in
NavigationLink {
DetailView(person: person)
} label: {
Text("\(person.last), \(person.first)")
}
}
}
.listStyle(.plain)
Spacer()
}
.sheet(isPresented: $sheetIsPresented) {
NavigationStack {
DetailView(person: Person())
}
}
}
}
#Preview {
ContentView()
.modelContainer(for: Person.self, inMemory: true)
}
AutoSave is on, but BTW I frequently add save code after, just to push changes out to watch updates in DB Browser in real-time (which the simulator doesn't seem to push & may otherwise loose if you quit before doing one of the "save-pushing" tasks like swiping to another app. e.g. follow the modelContext.insert(person) with:
guard let _ = try? modelContext.save() else {
print("😡 ERROR: Save on DetailView did not work.")
return
}
Thanks!
var person: Personwithout the@Statein yourDetailView.persistentModelIDproperty.