حزمة Core Data
أعد إعداد الحاوية الدائمة والسياق
حزمة Core Data درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
ماذا ستتعلم في «حزمة Core Data»؟
أعد إعداد الحاوية الدائمة والسياق تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟
لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «حزمة Core Data»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟
نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.