สร้างและบันทึกเอนทิตี
เพิ่มระเบียนและทำให้บริบทคงอยู่
สร้างและบันทึกเอนทิตี เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Adding New Data
Reading is half the story. To grow your app you also need to create new records and store them safely.
You Need the Context
Every new object is born inside a managed object context, so grab the context from the environment first.
@Environment(\.managedObjectContext) var contextMaking an Instance
Create an entity by initialising it with the context, which registers the new object for tracking.
let task = Task(context: context)Setting Attributes
Assign values to the object's attributes just like setting properties on any Swift object.
task.title = "Buy milk"
task.isDone = falseNothing Is Saved Yet
At this point the record only lives in memory. It becomes permanent when you save the context.
Calling save()
Persist all pending changes at once with a single save call, wrapped in a do-catch for safety.
do {
try context.save()
} catch {
print("Save failed: \(error)")
}Saving Is Transactional
One save writes every pending insert, edit, and delete together, so your store never lands half-updated.
Updating Existing Records
To edit, change the object's attributes and call save again. Core Data tracks what is dirty for you.
Deleting a Record
Remove an object with delete on the context, then save to make the removal stick.
context.delete(task)
try? context.save()Why Catch Errors
Saving can fail, for example if a required value is missing, so handling the error keeps your app from crashing.
Save After Each Action
A common habit is to save right after the user adds, edits, or deletes, so work is never lost on a sudden close.
Quick Check
You created and stored a record. One quick question about the flow.
Recap
You learned to create an entity with the context, set its attributes, and call save to insert, update, or delete records. ✨
คำถามที่พบบ่อย
บทเรียน “สร้างและบันทึกเอนทิตี” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “สร้างและบันทึกเอนทิตี” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “สร้างและบันทึกเอนทิตี”
เพิ่มระเบียนและทำให้บริบทคงอยู่ คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “สร้างและบันทึกเอนทิตี” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- สแตก Core Data
- ดึงข้อมูลด้วย @FetchRequest
- สร้างและบันทึกเอนทิตี
- เพรดิเคตและตัวบอกการเรียงลำดับ