-1

I have a master list -> detail view UI. Looks fine.

But when I show a detail view of one of the list element, the view sometimes supriously goes back to the list.

The master list can be updated in the background and when this happen, the list is re-ordered. I think that what is causing the "going back". According to this SO question SwiftUI Navigation View goes back in hierarchy and to this WWDC video, it seems my views identity are not constant.

How can I read and debug the view hierarchy (I mean SwiftUI view, not the corresponding UIViews) and their identity ?

Edit: changed the title to be more in line with the question.

AirXygène
  • 2,409
  • 15
  • 34
  • 1
    Please take the [tour](https://stackoverflow.com/tour) and read [How to Ask](https://stackoverflow.com/help/how-to-ask) to improve, edit and format your questions. Without a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) it is impossible to help you troubleshoot. – lorem ipsum Aug 12 '22 at 11:39
  • Thanks for your comment, but please read the question. It is not about the code, but about how to debug the SwiftUI view hierarchy, which is code agnostic. The question is tagged swiftui and debugging. Admittedly, the title is not good, I'll change it. – AirXygène Aug 12 '22 at 13:14
  • SwiftUI is not like UIKit you cannot access the "view hierarchy" the issue you are having is likely to misunderstanding how SwiftUI `View`s work. 90% the answer to this issue is that you should be using `@StateObject` instead of `@ObservedObject` to initialize the `ObservableObject` but it could be a many number of things. – lorem ipsum Aug 12 '22 at 13:26

2 Answers2

1

It seems there is no way to observe the SwiftUI view hierarchy or get a SwiftUI view identity.

However, modifiers like .onAppear and .onDisappear with debug prints inside are the tool that helped me finding out where views were being re-created instead of being kept.

The ultimate reason was that I had a UUID() left somewhere.

I highly recommend this WWDC Video. It explains the lifecycle of SwiftUI views.

AirXygène
  • 2,409
  • 15
  • 34
0

You can use:

    var body: some View {
#if DEBUG
        let _ = Self._printChanges()
#endif

in your body definition to find out why this view is redrawn.

Marc
  • 13
  • 5