Creare e salvare le entità
Inserisca i record e renda persistente il contesto.
Creare e salvare le entità è una lezione SwiftUI Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento SwiftUI Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso SwiftUI Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Adding New Data
Reading is half the story. To grow your app you also need to create new records and store them safely.
You Need the Context
Every new object is born inside a managed object context, so grab the context from the environment first.
@Environment(\.managedObjectContext) var contextMaking an Instance
Create an entity by initialising it with the context, which registers the new object for tracking.
let task = Task(context: context)Setting Attributes
Assign values to the object's attributes just like setting properties on any Swift object.
task.title = "Buy milk"
task.isDone = falseNothing Is Saved Yet
At this point the record only lives in memory. It becomes permanent when you save the context.
Calling save()
Persist all pending changes at once with a single save call, wrapped in a do-catch for safety.
do {
try context.save()
} catch {
print("Save failed: \(error)")
}Saving Is Transactional
One save writes every pending insert, edit, and delete together, so your store never lands half-updated.
Updating Existing Records
To edit, change the object's attributes and call save again. Core Data tracks what is dirty for you.
Deleting a Record
Remove an object with delete on the context, then save to make the removal stick.
context.delete(task)
try? context.save()Why Catch Errors
Saving can fail, for example if a required value is missing, so handling the error keeps your app from crashing.
Save After Each Action
A common habit is to save right after the user adds, edits, or deletes, so work is never lost on a sudden close.
Quick Check
You created and stored a record. One quick question about the flow.
Recap
You learned to create an entity with the context, set its attributes, and call save to insert, update, or delete records. ✨
Domande Frequenti
La lezione «Creare e salvare le entità» è gratuita?
Sì — il testo completo di «Creare e salvare le entità» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso SwiftUI Academy, passa a CoddyKit PRO. Il corso SwiftUI Academy include 4 lezioni in totale.
Cosa imparerò in «Creare e salvare le entità»?
Inserisca i record e renda persistente il contesto. Eserciti SwiftUI Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare SwiftUI Academy?
Non è richiesta alcuna esperienza precedente. SwiftUI Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Creare e salvare le entità»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione SwiftUI Academy?
Sì. Ogni lezione SwiftUI Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Lo stack Core Data
- Recuperare dati con @FetchRequest
- Creare e salvare le entità
- Predicati e descrittori di ordinamento