See How Many Times Your SwiftUI Views Redraw Using Instruments

If you run into a performance problem in your SwiftUI app, the cause could be your app’s views redrawing too often. Apple provides instruments in the Instruments app that let you see how many times SwiftUI draws your app’s views and the view properties that trigger redraws. This article shows how to use those instruments.

Profile Your App

Take the following steps to profile your app with the SwiftUI instruments:

  1. Go to Xcode.
  2. Press Cmd-I or choose Product > Profile to build your project and launch Instruments.
  3. Choose SwiftUI from the template list.
  4. Click the Choose button.
  5. Click the Record button above the sidebar to start profiling.
  6. Click the Stop button when you are finished.

The SwiftUI template has the following instruments:

  • View Body
  • View Properties
  • Core Animation Commits
  • Time Profiler
  • Hangs

I cover the View Body and View Properties instruments in this article. Read the following article to learn more about the Time Profiler instrument:

Finding the Slow Spots in Your Code with the Time Profiler Instrument

See the Number of View Redraws

Use the View Body instrument to see statistics about the redraws of the SwiftUI views in your app. The View Body instrument table has the following columns of data:

  • Count
  • Total Duration
  • Minimum Duration
  • Average Duration
  • Maximum Duration
  • Standard Deviation Duration

The most important columns are Count, Total Duration, and Average Duration. The Count column tells you how many times SwiftUI drew the view. The Total Duration column tells you the total time of those draws. The Average Duration column tells you the average time of each draw.

The View Body instrument table has at least two rows: one for SwiftUI and one for your app. Start by looking at the statistics for your app. You will see the total number of times SwiftUI drew your app’s views.

To see what views were drawn the most, click the disclosure triangle next to your app. There will be a row for each view in your SwiftUI app.

App view stats

The views that were drawn the most should appear at the top. If you don’t see the views that were drawn the most, click the Count table header to sort the views by count.

See the Properties that Trigger Redraws

If you find one of your views is being redrawn often, you want to figure out what is causing the view to redraw so much. How do you find the cause of redraws?

SwiftUI views redraw when the data bound to the views changes. Use the View Properties instrument to see the properties (variables) that change the most. Anything in your code that uses property wrappers like @State and Binding will appear in this instrument.

When you select the View Properties instrument, you will see a long list of properties with their current values. You can get more useful information from the summary. Press Cmd-2 or choose Summary from the jump bar to see the summary.

The summary looks similar to the summary for the View Body instrument. There are rows for SwiftUI and your app. There is one column that tells you the number of updates.

View property app stats

Where the View Properties instrument differs is it has levels you can drill down by clicking disclosure triangles. The View Properties instrument has the following levels:

  • View type
  • Property type
  • Property value

The view types for your app are your app’s views. They should be the same as the views in the View Body instrument.

The property types are the data types for the properties in your SwiftUI views. An example of a property type in a SwiftUI view is Binding<String>, which is a binding to a string. The instrument shows the data types, not the names of your variables.

The property values show the different values the property had during profiling. A Boolean property will show the number of times it was set to true and set to false.

You are noticing there’s a lot of disclosure triangle clicking when looking at view properties. Option-clicking a disclosure triangle will expand the list to show all the levels so you don’t have to click as much. Option-clicking again will contract the list.

Do you want to learn more about Instruments?

I’m writing a book that shows you how to use Instruments to find and fix problems in your code. The book will help you make apps that run faster and use less memory. You can learn more about the book and get updates on the book’s progress at the following link:

Instruments book page

Get the Swift Dev Journal Newsletter

Subscribe and get exclusive articles, a free guide on moving from tutorials to making your first app, notices of sales on books, and anything I decide to add in the future.

    We won't send you spam. Unsubscribe at any time.