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.
List
instead of theNavigationStack
? Without reproducible code, we cannot test any of this ourselves. – Andrei G. Commented Mar 24 at 0:25