Unexpected UI Freeze When Fetching Large Data Sets with SwiftUI in iOS 17
I'm experiencing a significant UI freeze when attempting to fetch large data sets in my SwiftUI application on iOS 17. The data is being retrieved from a REST API, and I'm using an `@State` variable to hold the data once it's fetched. The freeze lasts for a few seconds, making the app feel unresponsive during the fetch. I've tried moving the network call to a separate background thread using `DispatchQueue.global()`, but the issue persists. Here's a simplified version of my code: ```swift import SwiftUI struct ContentView: View { @State private var data: [MyDataModel] = [] @State private var isLoading = false var body: some View { VStack { if isLoading { ProgressView("Loading...") } else { List(data) { item in Text(item.title) } } } .onAppear { fetchData() } } func fetchData() { isLoading = true DispatchQueue.global(qos: .background).async { // Simulating a network request guard let url = URL(string: "https://api.example.com/data") else { return } let (data, _) = try? await URLSession.shared.data(from: url) let decodedData = try? JSONDecoder().decode([MyDataModel].self, from: data) DispatchQueue.main.async { self.data = decodedData ?? [] self.isLoading = false } } } } struct MyDataModel: Identifiable, Codable { let id: Int let title: String } ``` I suspect that the large payload is causing the UI to freeze during the decoding phase, but I haven't found a way to handle this efficiently. I'm considering using `@MainActor`, but I'm unsure where to implement it for optimal performance. Has anyone else encountered this issue, and what strategies did you use to mitigate UI freezes when dealing with large data sets in SwiftUI?