0Pricing
SwiftUI Academy · Lesson

Inserting, Updating & Deleting

Mutate the store and reflect changes live.

Inserting, Updating & Deleting is a free SwiftUI Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Changing Your Data

Now you will change the store: add new records, edit existing ones, and remove what you no longer need. ✏️

Inserting a Record

Create an object and call insert on the context to add it. SwiftData starts tracking it for saving right away.

let task = Task(title: "Walk dog")
context.insert(task)

Insert Then Appears

After an insert, any @Query view shows the new row at once. You do not refresh the list yourself.

Updating a Property

To edit a record, just change its property on the tracked object. SwiftData notices and saves the new value.

task.isDone = true

No Special Edit Call

There is no separate update method. Because the object is tracked, direct assignment is the whole update flow.

task.title = "Walk the dog twice"

Deleting a Record

Remove an object with delete on the context. It disappears from the store and from every query view.

context.delete(task)

Deleting from a List

Wire up swipe-to-delete with onDelete, then call delete for each index. Users remove rows with a familiar gesture.

.onDelete { offsets in
    for i in offsets {
        context.delete(tasks[i])
    }
}

Saving the Changes

SwiftData autosaves, but you can call save to commit immediately. It is wise before the app might close.

try context.save()

Batch Operations

Loop over a set of objects to insert or delete many at once. The context batches the work efficiently for you.

for t in finished {
    context.delete(t)
}

Handling Errors

Wrap a manual save in do-catch so a failure does not crash your app. You can warn the user instead.

do { try context.save() }
catch { print(error) }

Changes Flow to the UI

Every insert, edit, and delete reflects live in your views. The store and screen stay perfectly in step.

Quick Check

Quick check on editing data.

Recap: Editing Data

You can now insert, edit by assignment, and delete records, with the UI updating live. You have full control over your SwiftData store. 🚀

Frequently asked questions

Is the “Inserting, Updating & Deleting” lesson free?

Yes — the full text of “Inserting, Updating & Deleting” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.

What will I learn in “Inserting, Updating & Deleting”?

Mutate the store and reflect changes live. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SwiftUI Academy?

No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Inserting, Updating & Deleting” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SwiftUI Academy lesson?

Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining @Model Types
  2. The Model Container & Context
  3. Querying with @Query
  4. Inserting, Updating & Deleting
← Back to SwiftUI Academy