SwiftUI insists on re-creating view hiearchy on navbar clicks

I am building an app which displays a proprietary format document.

The document, as well as the custom UIView, is designed for user interaction.

I am using the navigation bar / toolbar to provide commands for interacting with the document (adding, removing objects, etc).

However, on each click/tap on the toolbar, swiftUI is recreating the views. This is totally unnecessary and messes up all data.

Note that there are no @State variables being changed, methods of the document (a pure swift class) are called directly in the action enclosure.

How can I stop swiftui from doing this ?

To solve this problem, you'll need to dig deeper into the way View lifetimes work, explained in this WWDC video:

https://developer.apple.com/videos/play/wwdc2021/10022/

I recommend you watch the entire video, but the section starting at time 32m 25s is particular relevant to the behavior you're asking about.

SwiftUI insists on re-creating view hiearchy on navbar clicks
 
 
Q