สแตก Core Data
ตั้งค่าคอนเทนเนอร์และบริบทถาวร
สแตก Core Data เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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. ✨
คำถามที่พบบ่อย
บทเรียน “สแตก Core Data” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “สแตก Core Data” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “สแตก Core Data”
ตั้งค่าคอนเทนเนอร์และบริบทถาวร คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “สแตก Core Data” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ