0Pricing
SwiftUI Academy · درس

الجلب باستخدام @FetchRequest

حمّل الكائنات المُدارة مباشرةً إلى الواجهات

الجلب باستخدام @FetchRequest درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في SwiftUI Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Reading Records in Views

To show saved data, a SwiftUI view needs to fetch it from Core Data and re-render whenever that data changes.

The @FetchRequest Wrapper

The @FetchRequest property wrapper runs a query for an entity and keeps the results live and current for you.

@FetchRequest(sortDescriptors: []) var tasks: FetchedResults<Task>

What You Get Back

Results arrive as FetchedResults, a collection you can loop over just like an array inside your view body.

Showing Them in a List

Drop the results straight into a List with ForEach to render one row per stored record.

List(tasks) { task in
    Text(task.title ?? "Untitled")
}

Sorting the Results

Pass sortDescriptors so rows arrive in a predictable order instead of a random one.

@FetchRequest(sortDescriptors: [SortDescriptor(\.title)]) var tasks: FetchedResults<Task>

Filtering with a Predicate

Add an NSPredicate to fetch only the records that match a rule, like done items or a search term.

@FetchRequest(sortDescriptors: [], predicate: NSPredicate(format: "isDone == true")) var done: FetchedResults<Task>

It Stays Live

@FetchRequest watches the context, so when you add or delete a record the view updates automatically. ⚡

It Needs the Context

@FetchRequest reads the context from the environment, so the value you injected in the stack must be present.

Handling an Empty Result

When the fetch returns nothing, show a friendly empty state instead of a blank screen.

if tasks.isEmpty {
    Text("No tasks yet")
}

Optional Attributes

Core Data attributes are often optional in Swift, so unwrap them safely before you display their values.

Declare It as var

Declare a @FetchRequest property as var, since the wrapper updates its stored results as the data changes.

Quick Check

You wired up @FetchRequest. A quick check on how it behaves.

Recap

You used @FetchRequest to pull records into a view, sort and filter them, and let the list refresh itself as data changes. ✨

الأسئلة الشائعة

هل درس «الجلب باستخدام @FetchRequest» مجاني؟

نعم — نص درس «الجلب باستخدام @FetchRequest» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.

ماذا ستتعلم في «الجلب باستخدام @FetchRequest»؟

حمّل الكائنات المُدارة مباشرةً إلى الواجهات تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟

لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «الجلب باستخدام @FetchRequest»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟

نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. حزمة Core Data
  2. الجلب باستخدام @FetchRequest
  3. إنشاء الكيانات وحفظها
  4. المسندات الشرطية ومسندات الترتيب
← العودة إلى SwiftUI Academy