Stos Core Data
Skonfiguruj trwały kontener i kontekst
Stos Core Data to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej SwiftUI Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. ✨
Często zadawane pytania
Czy lekcja „Stos Core Data” jest bezpłatna?
Tak — pełny tekst „Stos Core Data” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu SwiftUI Academy, przejdź na CoddyKit PRO. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Stos Core Data”?
Skonfiguruj trwały kontener i kontekst Ćwiczysz SwiftUI Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć SwiftUI Academy?
Nie wymagamy żadnego doświadczenia. SwiftUI Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.
Ile czasu zajmuje lekcja „Stos Core Data”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji SwiftUI Academy?
Tak. Każda lekcja SwiftUI Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Stos Core Data
- Pobieranie za pomocą @FetchRequest
- Tworzenie i zapisywanie encji
- Predykaty i deskryptory sortowania