UPDATED: I determined the line causing the hang was .animation(.default, value: layout)
.
I'm seeing a strange issue when switching between a ScrollView/LazyVGrid and a List with my SwiftData but when toggling the layout it ends up freezing and I can't confirm what's causing the app to hang since there's no crash. I'm not getting much info from the debugger. Any help would be appreciated.
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@Query private var items: [Item]
let gridItemLayout = [ GridItem(.adaptive(minimum: 150))]
@State private var layout = Layout.grid
var body: some View {
NavigationStack {
ZStack {
if layout == .grid {
ScrollView {
LazyVGrid(columns: gridItemLayout, spacing: 5) {
ForEach(items) { item in
}
}
}
} else {
List {
ForEach(items) { item in
}
}
}
}
// MARK: HERE'S THE ERROR
.animation(.default, value: layout)
.navigationTitle("ScrollView")
.toolbar {
ToolbarItemGroup {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
Menu {
Picker("Layout", selection: $layout) {
ForEach(Layout.allCases) { option in
Label(option.title, systemImage: option.imageName)
.tag(option)
}
}
.pickerStyle(.inline)
} label: {
Label("Layout Options", systemImage: layout.imageName)
.labelStyle(.iconOnly)
}
}
}
}
}
}
@Model
public class Item: Codable {
public let id: String
public enum CodingKeys: String, CodingKey {
case id
}
public init(id: String) {
self.id = id
}
required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(String.self, forKey: .id)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
}
}