My SwiftUI app has a network log screen that shows network requests sent and recorded by the app. There is a button to toggle the visibility of each request. Requests are serialized and stored as strings, so they can be displayed on this screen.
Some of the response objects are very large — as response objects tend to be sometimes — which is causing a 1-2 second delay when the “toggle visibility” button is pressed. Is there a way to optimize the performance of the Text view that renders the content?
struct NetworkLogsScreen: View {
var logs: [NetworkLogEntry]
var body: some View {
ScrollView {
LazyVStack {
ForEach(logs.indices.reversed(), id: \.self) { index in
LogItem(logItem: logs[index])
}
}
}
.navigationTitle("Network Requests")
}
}
struct LogItem: View {
var logItem: NetworkLogEntry
@State var isExpanded: Bool = false
var body: some View {
VStack {
HStack {
Text(logItem.timestamp.formatted())
Spacer()
Button {
self.isExpanded.toggle()
} label: {
Text("toggle visibility")
}
}
if self.isExpanded {
Text(logItem.responseBody)
}
}
}
}