Seems like a bug in SwiftUI. Any ideas? This is on macOS 11.[error] precondition failure: invalid value type for attribute: 230424 (saw PreferenceKeys, expected ViewList) AttributeGraph precondition failure: invalid value type for attribute: 230424 (saw PreferenceKeys, expected ViewList).
Replies
Code Block Swift ... List { ForEach(filteredThings, id: \.self) { item in itemRow(item: item, matched: $matched) } } ...
This worked for me:
Code Block ... List { ForEach(filteredThings, id: \.self) { item in itemRow(item: item, matched: $matched) } }.id(UUID()) ...
-
I know it had to be an id related issue. I was just thinking about to add id fields in my objects. Such a simple solution which worked like magic. Thanks!
-
!!Thanks!! for this simple but effective solutions. But having this kind of knowledge while building apps I normally would like to avoid. I hope, that SwiftUI will get rid of this kind of tricks.
Hi, I experienced this horrible problem and in my case was when using TabView.
I have TabView in my ContentView and then one of the List views inside had a NavigationView At the beginning I thought it was something wrong with my code cos was working previously. But after struggling a while. I moved the NavigationView to wrap the whole TabView and that fixed the issue.
So I will write here both examples the Non working one and the fix I did in case that might help others.
Problematic Code
struct ContentView: View {
@EnvironmentObject var dataModel: DataModel
var body: some View {
TabView {
LibraryList()
.tabItem {
Label(L10n.library, systemImage: Images.System.library)
}
SettingsList()
.tabItem {
Label(L10n.more, systemImage: Images.System.more )
}
}
}
}
struct LibraryList: View {
@EnvironmentObject var dataModel: DataModel
var body: some View {
NavigationView { // This navigation here seems to be the issue
List(dataModel.books) { book in
NavigationLink(destination: Text("Book")) { // The crash seems to complain here.
Text("Book name: \(book.name)")
}
}
}
}
}
The working solution
struct ContentView: View {
@EnvironmentObject var dataModel: DataModel
var body: some View {
NavigationView { // Moved navigation view before TabView.
TabView {
LibraryList()
.tabItem {
Label(L10n.library, systemImage: Images.System.library)
}
SettingsList()
.tabItem {
Label(L10n.more, systemImage: Images.System.more )
}
}
}
}
}
struct LibraryList: View {
@EnvironmentObject var dataModel: DataModel
var body: some View {
List(dataModel.books) { book in
NavigationLink(destination: Text("Book")) { // The crash seems to complain here.
Text("Book name: \(book.name)")
}
}
}
}
I hope this helps some of you.
The above solutions didn't work for me, but I figured it out.
I had a code that nests ForEach inside LazyVStack inside ScrollView inside NavigationView inside TabView.
Quickly scrolling the list would result in:
AttributeGraph precondition failure: setting value during update: 2232
Simple exchanging the order of NavigationView and TabView did not work for me.
What I did was to add .id(UUID()) to each tab views:
TabView {
MyView()
.id(UUID())
.tabItem {
I didn't have to swap NavigationView and TabView nesting order.
It worked!
-
—
NickZ
-
—
iphonetune
Add a CommentSame happens to me with a
LazyVGrid, when I add and remove array items repeatedly (doesn't have to be fast), but only when usingwithAnimation. My use case is a "See All" / "Show Less" toggle.In the button action:
withAnimation(.easeInOut(duration: 0.3)) { showAll.toggle() }Without
withAnimationI can toggle 100 times and still no crash.I had a NavigationView inside a NavigationView, removing the it from the child fixed it.