0Pricing
SwiftUI Academy · Pelajaran

Container & Context Model

Siapkan penyimpanan dengan modelContainer.

Container & Context Model adalah pelajaran SwiftUI Academy gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar SwiftUI Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus SwiftUI Academy mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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 context

Inserting 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. ✅

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Container & Context Model” gratis?

Ya — teks lengkap “Container & Context Model” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus SwiftUI Academy, upgrade ke CoddyKit PRO. Kursus SwiftUI Academy mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Container & Context Model”?

Siapkan penyimpanan dengan modelContainer. Kamu berlatih SwiftUI Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai SwiftUI Academy?

Tidak diperlukan pengalaman sebelumnya. SwiftUI Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.

Berapa lama pelajaran “Container & Context Model” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran SwiftUI Academy ini?

Ya. Setiap pelajaran SwiftUI Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Mendefinisikan Tipe @Model
  2. Container & Context Model
  3. Melakukan Query dengan @Query
  4. Menyisipkan, Memperbarui & Menghapus
← Kembali ke SwiftUI Academy