admin管理员组文章数量:1401233
I'm experiencing an issue where using @Query
in SwiftUI causes a .sheet(item:content:)
to be invoked twice when setting the item.
Here’s a simplified version of my code:
struct TodayPage: View {
@Environment(\.modelContext) private var modelContext
@FocusState private var focusedToDo: ToDoModel?
@State private var editingToDo: ToDoModel?
@Query(
filter: #Predicate<ToDoModel> { $0.isRepeat },
sort: \.order
)
private var toDos: [ToDoModel]
private var visibleToDos: [ToDoModel] {
let sortedToDos = toDos.sorted(option: .sortByGoal)
return sortedToDos.filter { $0.repeatDays.contains(.now) }
}
var body: some View {
NavigationStack {
GeometryReader { proxy in
if visibleToDos.isEmpty {
ContentUnavailableView(
LocalizedString.Today.emptyTitle,
systemImage: emptyImageName
)
.background(Color.background)
} else {
List {
ForEach(visibleToDos) { toDo in
HStack(alignment: .top, spacing: 10) {
// other views...
VStack(alignment: .leading, spacing: 8) {
HStack {
// other views...
if focusedToDo == toDo {
Button {
editingToDo = toDo // 1. Change
} label: {
Image(systemName: "info.circle")
.resizable()
.frame(width: 20, height: 20)
}
}
}
// other views...
}
}
}
}
}
}
}
// 2. Show a sheet
.sheet(item: $editingToDo) { toDo in
print("ToDoFormSheet - editingToDo : \(editingToDo) / toDo : \(toDo)")
return ToDoFormSheet(toDo: toDo)
}
}
}
When I tapped the button at // 1
, content
closure of sheet
modifier invoked twice.
ToDoFormSheet - editingToDo : nil / toDo : Blocus.SchemaV2.ToDoModel
ToDoFormSheet - editingToDo : Optional(Blocus.SchemaV2.ToDoModel) / toDo : Blocus.SchemaV2.ToDoModel
I have confirmed that using FetchDescriptor
instead of @Query
prevents this problem. However, this approach introduces other side effects, so I would prefer not to use it if possible.
I spend a lot of time for this but I found no clues why this happened and how to resolve this problem. I'm so exhausted now, please help me.
I'm experiencing an issue where using @Query
in SwiftUI causes a .sheet(item:content:)
to be invoked twice when setting the item.
Here’s a simplified version of my code:
struct TodayPage: View {
@Environment(\.modelContext) private var modelContext
@FocusState private var focusedToDo: ToDoModel?
@State private var editingToDo: ToDoModel?
@Query(
filter: #Predicate<ToDoModel> { $0.isRepeat },
sort: \.order
)
private var toDos: [ToDoModel]
private var visibleToDos: [ToDoModel] {
let sortedToDos = toDos.sorted(option: .sortByGoal)
return sortedToDos.filter { $0.repeatDays.contains(.now) }
}
var body: some View {
NavigationStack {
GeometryReader { proxy in
if visibleToDos.isEmpty {
ContentUnavailableView(
LocalizedString.Today.emptyTitle,
systemImage: emptyImageName
)
.background(Color.background)
} else {
List {
ForEach(visibleToDos) { toDo in
HStack(alignment: .top, spacing: 10) {
// other views...
VStack(alignment: .leading, spacing: 8) {
HStack {
// other views...
if focusedToDo == toDo {
Button {
editingToDo = toDo // 1. Change
} label: {
Image(systemName: "info.circle")
.resizable()
.frame(width: 20, height: 20)
}
}
}
// other views...
}
}
}
}
}
}
}
// 2. Show a sheet
.sheet(item: $editingToDo) { toDo in
print("ToDoFormSheet - editingToDo : \(editingToDo) / toDo : \(toDo)")
return ToDoFormSheet(toDo: toDo)
}
}
}
When I tapped the button at // 1
, content
closure of sheet
modifier invoked twice.
ToDoFormSheet - editingToDo : nil / toDo : Blocus.SchemaV2.ToDoModel
ToDoFormSheet - editingToDo : Optional(Blocus.SchemaV2.ToDoModel) / toDo : Blocus.SchemaV2.ToDoModel
I have confirmed that using FetchDescriptor
instead of @Query
prevents this problem. However, this approach introduces other side effects, so I would prefer not to use it if possible.
I spend a lot of time for this but I found no clues why this happened and how to resolve this problem. I'm so exhausted now, please help me.
Share Improve this question asked Mar 23 at 14:30 joeydevjoeydev 132 bronze badges 4 |1 Answer
Reset to default 0The sheet closure uses editingToDo which is self.editingToDo and the closure will be called again whenever self changes. Nothing wrong with that it's how SwiftUI is designed to work.
I would recommend doing your content unavailable in an overlay eg
List {
}
.overlay {
if empty {
Don't have if inside List rows. It will cause scroll performance issues cause it is unable to calculate the views per row.
本文标签: swiftdataSwiftUI Query causes sheet(itemcontent) to be called twiceStack Overflow
版权声明:本文标题:swiftdata - SwiftUI @Query causes .sheet(item:content:) to be called twice - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744283229a2598755.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
List
instead of theNavigationStack
? Without reproducible code, we cannot test any of this ourselves. – Andrei G. Commented Mar 24 at 0:25