Understanding Data Flow in SwiftUI

In this tutorial, you’ll learn how data flow in SwiftUI helps maintain a single source of truth for the data in your app. By Keegan Rush.

4.8 (32) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Environment Variables

In UserView.swift, add this line along with the rest of the property declarations:

@Environment(\.presentationMode) var presentationMode

This new property wrapper, @Environment, is like @EnvironmentObject in one way: both are shared across a view hierarchy. The difference is that you can add anything as an environment object, but environment values are more like key-value pairs.

While both @EnvironmentObject and @Environment share the environment, they serve very different purposes. You usually use @EnvironmentObject to manage dependencies in your app. SwiftUI uses @Environment as a way to manage settings for views and their children. Each view comes with environment values you can use to change the behavior of views in the hierarchy. One of these values is presentationMode.

Add the following line to the end of updateUserInfo():

presentationMode.wrappedValue.dismiss()

The presentation mode is shared across a view hierarchy, which makes it an environment variable. SwiftUI uses the presentation mode to manage what is currently displaying. By calling dismiss(), the view hierarchy will dismiss the current view, which, in this case, is UserView.

Build and run. Update your user information and tap Update and see how the app now dismisses the user view.

List after User View Update

Observing Objects

To update the default genre when adding a movie, you need to get a reference to UserStore in AddMovie.

Open AddMovie.swift. Add the following code near the top of the view, directly under the declaration of movieStore:

@EnvironmentObject var userStore: UserStore

Next, find the closing brace of the NavigationView inside AddMovie‘s body. It should be the last closing brace before the end of body. Add the following view modifier:

.onAppear { genre = userStore.currentUserInfo?.favoriteGenre ?? "" }

This sets genre to the user’s favorite genre, if there is one. GenrePicker‘s selection then updates to the favorite genre, thanks to data flow.

Finally, build and run. Click the user icon and set a favorite genre for yourself. Click Update to save the changes.

Setting a favorite genre

Now, go to the Add Movie screen. You’ll see your favorite genre as the selected choice in the picker. Hooray!

Viewing Favorite Genre

Choosing the Right Property Wrapper

Data flow is a lot to take in at first, and you might be wondering how to choose the right tool for the job.

If you don’t need to observe something to respond to updates, a normal property works fine. If you want to refresh a view any time something changes, then make your decision using these questions:

Data Flow Decision Chart

Where to Go From Here?

Download the final project by using the Download Materials button at the top or bottom of this tutorial.

Congratulations! You’ve mastered the art of data flow in SwiftUI and its associated property wrappers. You learned about the concept of sources of truth, which you can now use to keep all your data up to date. You also identified which property wrapper to use for the correct situation.

If you want to learn more about SwiftUI, including a whole chapter on data flow, then the SwiftUI by Tutorials book has what you need.

You can also refer to Apple’s documentation on State and Data Flow.

Working with data in SwiftUI is easy once you understand data flow, and we hope this tutorial made things clearer. If you have any questions or comments, please join the forum discussion below!