Model Container ve Context
modelContainer ile depolamayı kurun.
Model Container ve Context, CoddyKit'te ücretsiz bir SwiftUI Academy dersidir. Bu, 4 dersinin 2. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, SwiftUI Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. SwiftUI Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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. ✅
Sıkça Sorulan Sorular
“Model Container ve Context” dersi ücretsiz mi?
Evet — “Model Container ve Context” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve SwiftUI Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. SwiftUI Academy kursu toplamda 4 dersten oluşur.
“Model Container ve Context” dersinde ne öğreneceğim?
modelContainer ile depolamayı kurun. SwiftUI Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
SwiftUI Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te SwiftUI Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 2. dersidir.
“Model Container ve Context” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu SwiftUI Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her SwiftUI Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- @Model Türleri Tanımlama
- Model Container ve Context
- @Query ile Sorgulama
- Ekleme, Güncelleme ve Silme