Trouble with iOS 16's SwiftUI NavigationLink causing advanced patterns when using async data loading
I'm sure I'm missing something obvious here, but I'm building a SwiftUI app targeting iOS 16 and I've encountered an scenario with `NavigationLink` while trying to navigate to a detail view that loads data asynchronously. The question arises when I click the `NavigationLink` before the data has fully loaded, leading to a crash with the behavior message: `Fatal behavior: Unexpectedly found nil while unwrapping an Optional value`. I've tried implementing a loading state, but it seems that the `NavigationLink` is still active even when the data isn't ready. Here's a simplified version of my code: ```swift struct ContentView: View { @State private var items: [Item] = [] @State private var isLoading: Bool = true var body: some View { NavigationView { List(items, id: \ .id) { item in NavigationLink(destination: DetailView(item: item)) { Text(item.name) } } .onAppear(perform: loadData) .navigationTitle("Items") } } private func loadData() { DispatchQueue.main.asyncAfter(deadline: .now() + 2) { items = [Item(id: 1, name: "Item 1"), Item(id: 2, name: "Item 2")] isLoading = false } } } struct DetailView: View { let item: Item? var body: some View { Text(item?.name ?? "Unknown Item") } } ``` I've tried checking whether `items` is empty and conditionally presenting the `NavigationLink`, but I'm still getting the same crash. Hereβs what I attempted: ```swift if !isLoading { NavigationLink(destination: DetailView(item: item)) { Text(item.name) } } else { Text("Loading...") } ``` This approach leads to the detail view being presented without the required data, which is not what I want. How can I ensure that the navigation only occurs when the data is fully loaded? Any best practices for handling this situation would be appreciated. Is there a better approach? For context: I'm using Swift on Linux. This is happening in both development and production on Ubuntu 20.04. Am I missing something obvious?