الإدراج والتحديث والحذف
عدّل مخزن البيانات واعكس التغييرات مباشرةً
الإدراج والتحديث والحذف درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في SwiftUI Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Changing Your Data
Now you will change the store: add new records, edit existing ones, and remove what you no longer need. ✏️
Inserting a Record
Create an object and call insert on the context to add it. SwiftData starts tracking it for saving right away.
let task = Task(title: "Walk dog")
context.insert(task)Insert Then Appears
After an insert, any @Query view shows the new row at once. You do not refresh the list yourself.
Updating a Property
To edit a record, just change its property on the tracked object. SwiftData notices and saves the new value.
task.isDone = trueNo Special Edit Call
There is no separate update method. Because the object is tracked, direct assignment is the whole update flow.
task.title = "Walk the dog twice"Deleting a Record
Remove an object with delete on the context. It disappears from the store and from every query view.
context.delete(task)Deleting from a List
Wire up swipe-to-delete with onDelete, then call delete for each index. Users remove rows with a familiar gesture.
.onDelete { offsets in
for i in offsets {
context.delete(tasks[i])
}
}Saving the Changes
SwiftData autosaves, but you can call save to commit immediately. It is wise before the app might close.
try context.save()Batch Operations
Loop over a set of objects to insert or delete many at once. The context batches the work efficiently for you.
for t in finished {
context.delete(t)
}Handling Errors
Wrap a manual save in do-catch so a failure does not crash your app. You can warn the user instead.
do { try context.save() }
catch { print(error) }Changes Flow to the UI
Every insert, edit, and delete reflects live in your views. The store and screen stay perfectly in step.
Quick Check
Quick check on editing data.
Recap: Editing Data
You can now insert, edit by assignment, and delete records, with the UI updating live. You have full control over your SwiftData store. 🚀
الأسئلة الشائعة
هل درس «الإدراج والتحديث والحذف» مجاني؟
نعم — نص درس «الإدراج والتحديث والحذف» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
ماذا ستتعلم في «الإدراج والتحديث والحذف»؟
عدّل مخزن البيانات واعكس التغييرات مباشرةً تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟
لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «الإدراج والتحديث والحذف»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟
نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تعريف أنواع @Model
- حاوية النموذج والسياق
- الاستعلام باستخدام @Query
- الإدراج والتحديث والحذف