skip to Main Content

I have a searchable list displaying various instances of a certain Data Model which are saved in an array. When tapping on one of the list rows a new view opens displaying some information about the Data Model. For demonstration purposes, the view opening displays a randomly generated Number. This works as expected up to this point.

However, when performing a search displaying multiple items and then performing a second search displaying only a subset of the first items, after tapping on the selected row, the NavigationLink pushes to the new view twice.

This problem is easy to replicate with very little code.
Here is my Data Model:

struct DataModel: Identifiable, Hashable {
let id = UUID()
var name: String

init(name: String = "unknown") {
    self.name = name
}

static func == (lhs: DataModel, rhs: DataModel) -> Bool {
    return lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
    hasher.combine(id)
}
}

And here is my View:

var allDataModels = [DataModel]()

struct ContentView: View {
@State var searchDataModels = [DataModel]()
@State var searchText = ""

let numbers = Array(1...10)

var body: some View {
    NavigationStack {
        List {
            ForEach(searchDataModels, id: .id) { model in
                NavigationLink(value: model, label: {
                    Text(model.name)
                })
            }
        }.searchable(text: $searchText)

            .onChange(of: searchText, perform: { _ in
                updateSearch()
            })
            
            .navigationDestination(for: DataModel.self, destination: { _ in
                Text("(Int.random(in: 1...100))")})
    }.onAppear {
        for i in 0...9 {
            allDataModels.append(DataModel(name: "Data (numbers[i])"))
        }
    }
}

func updateSearch() {
    searchDataModels = allDataModels.filter( { $0.name.localizedCaseInsensitiveContains(searchText) } )
}
}

The following video demonstrates the NavigationLink pushing to two Views after performing two searches. As is visible in the video, the numbers on the screen change, making the views easy to distinguish from one another.

Problem visualization

This Problem has been tested and occurs in iOS 16 beta 3 and persists in beta 4. Earlier versions were not tested (NavigationStack and .navigationDestination(for: , destination:) are new in iOS 16).

2

Answers


  1. Chosen as BEST ANSWER

    The problem seems to be resolved in iOS 16 beta 5.


  2. I am having the same problem in Xcode 14.2. I have NavigationStack with List showing CoreData from @FetchRequest. Strange thing is that it started to behave like this when I started storing Binary Data. Not sure if it’s just coinsidence. For the moment, I am "happy" with a "workaround" that I found at:

    https://developer.apple.com/forums/thread/711899?answerId=725008022#725008022

    All text below is copied from the before mentioned link.

    With Form .buttonStyle(BorderlessButtonStyle()) on the Stack or Button fixed the Issue.

    I think the Error comes from a kinda hit-test Layer problem with the Default Button in some Cases of Navigation Stack or Form. In my Form, without the modifier, all Buttons in a Stack are hit together. The modifier fixed it just by accident. On runtime, you just don’t get the correct root of the Error. It’s a strange Bug indeed

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search