samhastings1066
samhastings1066

Reputation: 1

Apple Instruments: View Body instrument does not record invocations of SwiftUI Text views

The following sample code is deliberately designed to cause a hang by making SwiftUI draw the Text("Child") view thousands of times at once (by wrapping ChildView in a VStack the views inside the child ForEach are created "eagerly" i.e. all at once):

import SwiftUI

struct ParentView: View {
    var body: some View {
        List {
            ForEach(0..<10) { parentIndex in
                VStack {
                    Text("Parent \(parentIndex + 1 )")
                    ChildView()
                }
            }
        }
        .padding()
    }
}

struct ChildView: View {
    var body: some View {
        ForEach(0..<1000) { childIndex in
            Text("Child")
                .frame(minHeight: 200)
        }
    }
}

When profiling this code a hang is generated (as expected). The output of the View Body instrument over the inspection range of this hang shows 95 total view body invocations in the SwiftUI lane and 8 in the NestedForEach (the name of my dummy app) lane:

Image of View Body output using Text view

From this article in Hacking With Swift, I am led to believe that the SwiftUI lane is for "primitive types like text views and buttons", but the invocations of the Text view are not logged there. I believe this is because, as this answer explains, a SwiftUI view's body isn't directly accessible at compile-time.

I adapt my code to use a custom view called TextView as follows:

import SwiftUI

struct ParentView: View {
    var body: some View {
        List {
            ForEach(0..<10) { parentIndex in
                VStack {
                    Text("Parent \(parentIndex + 1 )")
                    ChildView()
                }
            }
        }
        .padding()
    }
}

struct ChildView: View {
    var body: some View {
        ForEach(0..<1000) { childIndex in
            TextView()
                .frame(minHeight: 200)
        }
    }
}

struct TextView: View {
    var body: some View {
        Text("Child")
    }
}

By using the custom view I now get a more useful read out when I profile my app using the View Body instrument. In the NestedForEach lane I see that there were 5000 invocations of the body of my TextView view.

Image of View Body output using custom view

The problem is this: without creating the custom view I would never have discovered the cause of the hang (thousands of invocations of that custom view) using Instruments. Is there some way to view the invocation of SwiftUI views like Text using Instruments? Or is there another way to uncover the cause of such a hang without taking the unusual step of creating a custom view just to display a Text view?

Upvotes: 0

Views: 71

Answers (0)

Related Questions