0Pricing
SwiftUI Academy · Lezione

Lo stack Core Data

Configuri il contenitore persistente e il contesto.

Lo stack Core Data è una lezione SwiftUI Academy gratuita su CoddyKit. Questa è la lezione 1 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.

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

A 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. ✨

Domande Frequenti

La lezione «Lo stack Core Data» è gratuita?

Sì — il testo completo di «Lo stack Core Data» è 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 «Lo stack Core Data»?

Configuri il contenitore persistente e 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 1 di 4.

Quanto tempo richiede la lezione «Lo stack Core Data»?

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. Lo stack Core Data
  2. Recuperare dati con @FetchRequest
  3. Creare e salvare le entità
  4. Predicati e descrittori di ordinamento
← Torna a SwiftUI Academy