Tuesday, December 21, 2021

How to Find Why a SwiftUI View Is Updating

Luca Bernardi (via Dave Verwer):

SwiftUI has a new, pretty cool, debugging utility to help you understand what is causing a view to be reevaluated.

Call Self._printChanges() inside the body of a view to print out the changes that have triggered the view update.

Tim Wood:

This has been helpful a couple times already -- thanks! But unfortunately it seems to have some rough edges and can report false positives (FB9184397, FB9090652) in the latter report still evaluating the view when there are no changes (currently a big perf problem for us).

Paul Hudson:

[It] should be called inside the body property. This means you will temporarily need to add an explicit return to send back your regular view code.

To demonstrate this method in action, here’s some sample code where a view relies on an observable object that randomly issues change notifications:

1 Comment RSS · Twitter


You can use it in a view builder function if you write it like this (no need for adding an explicit return):

let _ = Self._printChanges()

Leave a Comment