0Pricing
SwiftUI Academy · Lezione

Il contenitore e il contesto del modello

Configuri l'archiviazione con modelContainer.

Il contenitore e il contesto del modello è una lezione SwiftUI Academy gratuita su CoddyKit. Questa è la lezione 2 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.

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

Domande Frequenti

La lezione «Il contenitore e il contesto del modello» è gratuita?

Sì — il testo completo di «Il contenitore e il contesto del modello» è 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 «Il contenitore e il contesto del modello»?

Configuri l'archiviazione con modelContainer. 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 2 di 4.

Quanto tempo richiede la lezione «Il contenitore e il contesto del modello»?

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

  1. Definire i tipi @Model
  2. Il contenitore e il contesto del modello
  3. Eseguire query con @Query
  4. Inserire, aggiornare ed eliminare
← Torna a SwiftUI Academy