0Pricing
SwiftUI Academy · บทเรียน

คอนเทนเนอร์และบริบทของโมเดล

ตั้งค่าที่จัดเก็บด้วย modelContainer

คอนเทนเนอร์และบริบทของโมเดล เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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

คำถามที่พบบ่อย

บทเรียน “คอนเทนเนอร์และบริบทของโมเดล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “คอนเทนเนอร์และบริบทของโมเดล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “คอนเทนเนอร์และบริบทของโมเดล”

ตั้งค่าที่จัดเก็บด้วย modelContainer คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “คอนเทนเนอร์และบริบทของโมเดล” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม

ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. กำหนดชนิด @Model
  2. คอนเทนเนอร์และบริบทของโมเดล
  3. ค้นหาด้วย @Query
  4. การเพิ่ม การอัปเดต และการลบ
← กลับไปที่ SwiftUI Academy