2

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.

3
  • 1
    Please take the tour and read How to Ask to improve, edit and format your questions. Without a Minimal Reproducible Example it is impossible to help you troubleshoot. Commented Aug 12, 2022 at 11:39
  • 2
    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. Commented Aug 12, 2022 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 Views 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. Commented Aug 12, 2022 at 13:26

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

1

Even for SwiftUI views, in Xcode there is this certain feature access like this: Debug>View Debugging>Capture View Hierarchy. I'm not totally sure however if it's possible to dig out the individual IDs of each view there.

1 Comment

Thanks, but I don't think the view capture tool allows to access view IDs. At least i couldn't find it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.