Logo for tanaschita.com

How to manage view lifecycle events in SwiftUI

Discover SwiftUI's view lifecycle events onAppear, onDisappear, task, and task(id:).

updated on 15 Sep 2025 · 3 min read

When working with SwiftUI views, we often need to trigger side effects when a view appears or disappears. We can do that by using declarative modifiers that attach to views.

Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE

The onAppear(perform:) method

onAppear runs right before a view appears. It is useful for lightweight setup or triggering updates.

struct SomeView: View {
var body: some View {
VStack {
...
}
.onAppear {
viewModel.update()
}
}
}

The onDisappear(perform:) method

onDisappear runs right after the view disappears. It's the place to clean up or cancel ongoing work.

struct SomeView: View {
var body: some View {
VStack {
...
}
.onDisappear {
viewModel.cancelLoading()
}
}
}

The task(priority:_:) method

The task modifier allows us to start asynchronous work when a view appears. SwiftUI automatically cancels the task when the view disappears, so it integrates neatly with the view lifecycle.

struct SomeView: View {
var body: some View {
VStack {
...
}
.task {
await viewModel.load()
}
}
}

The task(id:) variant

task(id:) is a more advanced form of task. It still behaves like the regular task in that it runs once when the view appears and is canceled when the view disappears. The difference is that it also observes a value we provide as an identifier. Whenever that value changes, SwiftUI cancels the old task and starts a new one automatically.

.task(id: viewModel.query) {
await viewModel.search(query: viewModel.query)
}

In this example, the search runs once when the view appears, and then again every time the query changes. This makes task(id:) especially useful for scenarios like search bars or dynamic filters, where you want to restart asynchronous work when a parameter updates, without having to manage cancellation manually.

With these tools, SwiftUI provides a clear way to run both synchronous and asynchronous work in response to view lifecycle changes.

Sponsorship logo
Preparing for a technical iOS job interview
Preparing for a technical iOS Job Interview with over 300 questions & answers. Covering Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, iOS File System, Core Data, Concurrency with async/await, Security, Automated Testing, Dependency Management, AI & Machine Learning and more.
LEARN MORE
Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE
Sponsorship logo
Become a sponsor of tanaschita.com
By publishing an article on different iOS topics every week, tanaschita.com is constantly growing in the developer community and may provide a great audience for you as a sponsor.
CLICK TO LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

activitykit

widgetkit

swiftui

swift

ios

Getting started with Live Activities in SwiftUI

Build a public transport journey tracker for the Lock Screen and Dynamic Island with ActivityKit.

07 Sep 2026 · 7 min read

Latest articles and tips

© 2026 tanaschita.com

Privacy policy

Impressum