Browse Questions
- Both run code when a view appears, but task {} is purpose-built for async work.
- SSL pinning validates that the server's certificate matches a known copy embedded in the app, preventing man-in-the-middle attacks even with valid certificates.
- All three create URLSessionTask subclasses but handle data differently.
- GeometryReader is a container view that exposes the size and coordinate space of its parent, letting you build size-dependent layouts.
Answer:
Both run code when a view appears, but task {} is purpose-built for async work.
onAppear | task | |
|---|---|---|
| Async support | ❌ No | ✅ Yes |
| Auto-cancels | ❌ No | ✅ Yes (when view disappears) |
| Retry on id change | ❌ No | ✅ With id: parameter |
Code Example:
struct ArticleView: View {
@State private var article: Article?
var body: some View {
Text(article?.title ?? "Loading...")
.task {
// automatically cancelled if view disappears
article = await ArticleService.fetch()
}
}
}