0Pricing
SwiftUI Academy · Aula

Inserindo, Atualizando e Excluindo

Altere o armazenamento e reflita as mudanças em tempo real.

Inserindo, Atualizando e Excluindo é uma aula grátis de SwiftUI Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de SwiftUI Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de SwiftUI Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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. 🚀

Perguntas Frequentes

A aula “Inserindo, Atualizando e Excluindo” é grátis?

Sim — o texto completo de “Inserindo, Atualizando e Excluindo” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de SwiftUI Academy, atualize para CoddyKit PRO. O curso de SwiftUI Academy inclui 4 aulas no total.

O que vou aprender em “Inserindo, Atualizando e Excluindo”?

Altere o armazenamento e reflita as mudanças em tempo real. Você pratica SwiftUI Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar SwiftUI Academy?

Nenhuma experiência prévia é necessária. SwiftUI Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.

Quanto tempo leva a aula “Inserindo, Atualizando e Excluindo”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de SwiftUI Academy?

Sim. Cada aula de SwiftUI Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Definindo Tipos @Model
  2. O Contêiner e o Contexto do Modelo
  3. Consultando com @Query
  4. Inserindo, Atualizando e Excluindo
← Voltar para SwiftUI Academy