Der Core-Data-Stack
Richten Sie den persistenten Container und den Kontext ein.
Der Core-Data-Stack ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 1 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.
Meet Core Data
Core Data is Apple's mature framework for saving structured app data on the device, so your records survive after the app closes. 💾
What the Stack Is
The Core Data stack is the small set of objects that work together to load your data model and read or write records.
The Data Model File
Your entities live in a .xcdatamodeld file, a visual schema where you define the types and attributes Core Data will store.
The Persistent Container
The NSPersistentContainer wraps the whole stack: it loads your model and sets up the underlying storage for you.
let container = NSPersistentContainer(name: "MyApp")Loading the Stores
Call loadPersistentStores to open the database. Its closure tells you whether the store opened or failed.
container.loadPersistentStores { _, error in
if let error { fatalError("\(error)") }
}The View Context
The viewContext is your in-memory scratchpad for objects on the main thread, where you create, edit, and read records.
let context = container.viewContextA Reusable Controller
Most apps wrap the stack in one small controller class, so the rest of the app shares a single, ready-to-use context.
Injecting the Context
Pass the context into SwiftUI through the environment, so any view can reach it without manual hand-offs.
.environment(\.managedObjectContext, context)Saving Work
Changes stay in memory until you call save on the context, which writes everything to disk in one step.
try context.save()Why One Stack
Sharing a single stack keeps every view looking at the same data, so nothing falls out of sync across screens.
Set It Up Early
Build the stack once at app launch and hold onto it, so the container is ready before any screen needs data.
Quick Check
You set up the stack. One quick question about the pieces.
Recap
You met the Core Data stack: a model file, a persistent container, and a view context you inject into SwiftUI to read and save data. ✨
Häufig gestellte Fragen
Ist die Lektion „Der Core-Data-Stack“ kostenlos?
Ja — der vollständige Text von „Der Core-Data-Stack“ 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 „Der Core-Data-Stack“?
Richten Sie den persistenten Container und den Kontext ein. 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 1 von 4.
Wie lange dauert die Lektion „Der Core-Data-Stack“?
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