0Pricing
SwiftUI Academy · Lektion

Der Model-Container und der Kontext

Richten Sie die Speicherung mit modelContainer ein.

Der Model-Container und der Kontext ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 2 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.

What a Container Is

The model container is the engine that sets up your storage on disk. Your whole app shares one container for all its SwiftData models.

Adding the Container

Attach a container to your app with the modelContainer modifier on a scene. One line gives every view access to your saved data.

WindowGroup {
    ContentView()
}
.modelContainer(for: Task.self)

Listing Multiple Models

Pass an array to register several models at once. The container then knows about every type your app needs to save.

.modelContainer(for: [Task.self, Project.self])

What a Context Is

The model context is your working space for changes. You insert, edit, and delete through it, then it saves to the container.

Reading the Context

Grab the active context inside a view with the @Environment property wrapper. SwiftData injects it for you automatically.

@Environment(\.modelContext) private var context

Inserting an Object

Call insert on the context to start tracking a new object. From this point SwiftData watches it for saving.

context.insert(Task(title: "Buy milk"))

Automatic Saving

SwiftData usually autosaves changes for you at smart moments. You rarely need to save by hand in a simple app.

Saving Manually

When you need certainty, call save on the context to write changes right now. It is handy before a critical step.

try context.save()

In-Memory Containers

Set isStoredInMemoryOnly to true for a throwaway store. It is perfect for previews and tests that should not touch the disk.

let config = ModelConfiguration(
    isStoredInMemoryOnly: true)

Custom Configuration

A ModelConfiguration lets you tune where and how data is stored. You pass it when building a container by hand.

let container = try ModelContainer(
    for: Task.self, configurations: config)

One Container, Many Views

Because the container lives at the app level, every view shares the same data. Edits in one screen show up everywhere instantly.

Quick Check

Quick check on container and context.

Recap: Container & Context

You set up storage with modelContainer, then insert and save through the context from @Environment. Your app can now read and write data. ✅

Häufig gestellte Fragen

Ist die Lektion „Der Model-Container und der Kontext“ kostenlos?

Ja — der vollständige Text von „Der Model-Container und der Kontext“ 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 Model-Container und der Kontext“?

Richten Sie die Speicherung mit modelContainer 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 2 von 4.

Wie lange dauert die Lektion „Der Model-Container und der Kontext“?

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

  1. @Model-Typen definieren
  2. Der Model-Container und der Kontext
  3. Mit @Query abfragen
  4. Einfügen, Aktualisieren und Löschen
← Zurück zu SwiftUI Academy