0Pricing
SwiftUI Academy · درس

السحب للتحديث

أعد تحميل البيانات باستخدام المعدِّل refreshable

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

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

Refreshing on Demand

Users expect to pull a list down to reload it. SwiftUI bakes this gesture in with a single modifier, no gesture code required. ✨

Meet refreshable

Attach the refreshable modifier to a List. Pulling down reveals a spinner and runs the async work you provide.

.refreshable {
    await reload()
}

It Expects Async Work

The refreshable closure is async, so it pairs perfectly with await and modern Swift concurrency for fetching data.

Spinner Tied to Your Task

The pull-to-refresh spinner stays visible until your await finishes. SwiftUI hides it the moment the work completes.

A Simple Reload

Inside refreshable, call an async function that fetches fresh data and updates your @State array on completion.

func reload() async {
    items = await fetchItems()
}

Awaiting a Network Call

A real reload usually awaits a network request. Use URLSession with async/await to pull new items from a server.

let (data, _) = try await
    URLSession.shared.data(from: url)

Update State After Await

Once the await returns, assign the decoded results to your @State array and the list refreshes with the new rows.

let fresh = try decoder.decode(
    [Item].self, from: data)
items = fresh

Handle Errors Gracefully

Network calls can fail. Wrap the work in do/catch so a dropped connection shows a message instead of crashing.

do {
    items = try await fetchItems()
} catch {
    showError = true
}

Refreshable Needs a List

The pull gesture only works on scrollable containers like List or ScrollView. Attaching refreshable to a VStack does nothing.

Keep It Quick

Refresh should feel snappy. If your fetch is slow, show progress and avoid blocking the main thread with heavy work.

Pairs With Other Editing

Pull-to-refresh sits happily beside onDelete, onMove, and add. Together they make a fully interactive, editable list. ✨

Quick Check

What kind of closure does refreshable take?

Recap: Pull-to-Refresh

You added reload-on-pull with the async refreshable modifier on a List, awaiting fresh data and handling errors. 🎉

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

هل درس «السحب للتحديث» مجاني؟

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

ماذا ستتعلم في «السحب للتحديث»؟

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

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

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

كم من الوقت يستغرق درس «السحب للتحديث»؟

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

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

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

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

  1. حذف الصفوف بالسحب
  2. إعادة الترتيب باستخدام onMove
  3. إضافة عناصر جديدة
  4. السحب للتحديث
← العودة إلى SwiftUI Academy