In the code below, if I click a row, Hello
, in the List, the List’s onDisappear()
is invoked, as indicated by the trace, onDisappear called!
. Is this expected? The behavior is inconsistent, however; I have similar code but without onDisappear
being called. It appears to be caused by some random combination of ForEach
and NavigationLink
.
I have some logic that needs to be executed when the List
view is dismissed by an explicit action from the users. Thus, the unexpected call to onDisappear()
would be problematic for me, if not accounted for.
Is it possible to prevent the framework from auto-invoking onDisappear
for the parent view?
import SwiftUI
import SwiftData
struct DebugOnDisappear: View {
var body: some View {
UnexpectedOnDisappear()
.onDisappear() {
print("onDisappear called!")
}
}
struct UnexpectedOnDisappear: View {
@Query private var sbEntries: [SafeBoxEntryModel] = []
var body: some View {
VStack {
List {
ForEach (sbEntries) { sbEntry in
NavigationLink("Hello") {Text("Hello")}
}
}
}
}
}
}
Screenshot for listing
Screenshot for trace output