Entities erstellen und speichern
Fügen Sie Datensätze ein und speichern Sie den Kontext.
Entities erstellen und speichern ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des SwiftUI Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. ✨
Häufig gestellte Fragen
Ist die Lektion „Entities erstellen und speichern“ kostenlos?
Ja — der vollständige Text von „Entities erstellen und speichern“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des SwiftUI Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Entities erstellen und speichern“?
Fügen Sie Datensätze ein und speichern Sie den Kontext. Du übst SwiftUI Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um SwiftUI Academy zu starten?
Keine Vorkenntnisse erforderlich. SwiftUI Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Entities erstellen und speichern“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser SwiftUI Academy-Lektion Code schreiben und ausführen?
Ja. Jede SwiftUI Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Der Core-Data-Stack
- Mit @FetchRequest abrufen
- Entities erstellen und speichern
- Prädikate und Sort Descriptors