Kontener i kontekst modelu
Skonfiguruj magazyn za pomocą modelContainer
Kontener i kontekst modelu to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 2 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.
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 contextInserting 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. ✅
Często zadawane pytania
Czy lekcja „Kontener i kontekst modelu” jest bezpłatna?
Tak — pełny tekst „Kontener i kontekst modelu” 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 „Kontener i kontekst modelu”?
Skonfiguruj magazyn za pomocą modelContainer Ć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 2 z 4.
Ile czasu zajmuje lekcja „Kontener i kontekst modelu”?
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
- Definiowanie typów @Model
- Kontener i kontekst modelu
- Wykonywanie zapytań za pomocą @Query
- Wstawianie, aktualizowanie i usuwanie