0Pricing
SwiftUI Academy · Lección

La pila de Core Data

Configure el contenedor y el contexto persistentes.

La pila de Core Data es una lección gratuita de SwiftUI Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de SwiftUI Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de SwiftUI Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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

Preguntas frecuentes

¿La lección «La pila de Core Data» es gratis?

Sí — el texto completo de «La pila de Core Data» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de SwiftUI Academy, actualiza a CoddyKit PRO. El curso de SwiftUI Academy incluye 4 lecciones en total.

¿Qué aprenderé en «La pila de Core Data»?

Configure el contenedor y el contexto persistentes. Practicas SwiftUI Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar SwiftUI Academy?

No se requiere experiencia previa. SwiftUI Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.

¿Cuánto tiempo toma la lección «La pila de Core Data»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de SwiftUI Academy?

Sí. Cada lección de SwiftUI Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. La pila de Core Data
  2. Obtener datos con @FetchRequest
  3. Crear y guardar entidades
  4. Predicados y descriptores de ordenación
← Volver a SwiftUI Academy