Skip to content
DeveloperMemos

Using SwiftUI's onDisappear

SwiftUI, onDisappear, iOS Development1 min read

In SwiftUI, the onDisappear modifier allows you to execute code when a view disappears from the screen. This can be useful for performing cleanup tasks, saving data, or updating the state before the view is no longer visible. In this article, we will explore how to use onDisappear effectively with some practical examples.

Basic Usage

The onDisappear modifier can be applied to any SwiftUI view and takes a closure as its parameter. The closure is executed when the view disappears from the screen. Here's a basic example:

1struct ContentView: View {
2 var body: some View {
3 Text("Hello, SwiftUI!")
4 .onDisappear {
5 print("View disappeared")
6 }
7 }
8}

In this example, when the ContentView is no longer visible, the closure inside onDisappear will be called, and "View disappeared" will be printed in the console.

Persisting Data

One common use case for onDisappear is to save or update data when a view disappears. Suppose you have an editing screen where the user can modify some data. You can use onDisappear to save the changes before the view is dismissed. Here's an example:

1struct EditProfileView: View {
2 @Environment(\.presentationMode) var presentationMode
3
4 @State private var name = ""
5 @State private var bio = ""
6
7 var body: some View {
8 VStack {
9 TextField("Name", text: $name)
10 TextField("Bio", text: $bio)
11
12 Button("Save") {
13 // Save the changes
14
15 // Dismiss the view
16 presentationMode.wrappedValue.dismiss()
17 }
18 .padding()
19 }
20 .onDisappear {
21 // Save the changes when the view disappears
22 saveChanges()
23 }
24 }
25
26 private func saveChanges() {
27 // Perform the necessary operations to save the changes
28 print("Saving changes...")
29 }
30}

In this example, the EditProfileView contains text fields for name and bio, along with a "Save" button. When the user taps the "Save" button, the changes are saved, and the view is dismissed. However, if the user navigates back without saving explicitly, the changes will still be saved automatically thanks to the onDisappear modifier.

Performing Cleanup Tasks

Another use case for onDisappear is performing cleanup tasks when a view disappears. For instance, you may have a view that opens a network connection or starts observing a data source. You can use onDisappear to close the connection or stop the observation when the view goes off-screen. Here's an example:

1struct NetworkDataView: View {
2 @ObservedObject var dataProvider: NetworkDataProvider
3
4 var body: some View {
5 List(dataProvider.data) { item in
6 Text(item.title)
7 }
8 .onAppear {
9 dataProvider.fetchData()
10 }
11 .onDisappear {
12 dataProvider.cancelFetching()
13 }
14 }
15}

In this example, the NetworkDataView displays a list of data fetched from a network provider. When the view appears, the dataProvider fetches the data. And when the view disappears, the dataProvider cancels the ongoing fetch operation to avoid unnecessary network requests.

Wrapping Up

The onDisappear modifier in SwiftUI is a powerful tool for executing code when a view disappears from the screen. It allows you to perform data persistence, cleanup tasks, or any custom logic you need when managing the lifecycle of your views. By utilizing onDisappear, you can enhance the functionality and user experience of your SwiftUI apps.