معدّل .task
حمّل البيانات عند ظهور الواجهة
معدّل .task درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في SwiftUI Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Loading When a View Appears
Often you want to fetch data the moment a screen shows. The .task modifier runs async work exactly when the view appears. ⏱️
Attaching .task
Add .task to any view and give it an async closure. SwiftUI starts that work as the view comes on screen.
List(users) { user in Text(user.name) }
.task { await load() }It Calls Async Code
Inside .task you can freely use await. That makes it the natural home for your URLSession fetch on appearance.
.task {
users = try? await loadUsers()
}Why Not onAppear
The older onAppear cannot await async work directly. The .task modifier is built for concurrency, so prefer it for loading.
Automatic Cancellation
When the view disappears, SwiftUI cancels the task for you. That stops wasted work and avoids updating a gone view. ✨
Reacting to an id
Pass an id to .task and it reruns whenever that value changes. Perfect for reloading when a selection updates.
.task(id: selectedID) {
await loadDetail(selectedID)
}Updating State After Fetch
Store results in @State so assigning them refreshes the UI. The view redraws as soon as the data arrives.
@State private var users: [User] = []Handling Errors Inside
Wrap risky calls in do/catch inside the task. You can then set an error message that the view shows the user.
do { users = try await loadUsers() }
catch { errorText = "Failed" }Runs on the Main Actor
A .task closure starts on the main actor, so updating state and UI from it is safe without extra hopping.
One Task per Lifetime
By default .task runs once per appearance. For repeated refresh use pull-to-refresh or a changing id instead.
Keeping the Closure Small
Call a named function from .task rather than stuffing logic inline. It keeps the view tidy and easy to read.
.task { await viewModel.refresh() }Quick Check
Quick check on triggering async loads.
Recap: .task
You learned that .task runs async work when a view appears, cancels on disappear, can rerun on an id, and is the go-to place to fetch. 👍
الأسئلة الشائعة
هل درس «معدّل .task» مجاني؟
نعم — نص درس «معدّل .task» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
ماذا ستتعلم في «معدّل .task»؟
حمّل البيانات عند ظهور الواجهة تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟
لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «معدّل .task»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟
نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.