最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swiftdata - SwiftUI @Query causes .sheet(item:content:) to be called twice - Stack Overflow

programmeradmin6浏览0评论

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
  • Could you create an example that is reproducible? – Joakim Danielson Commented Mar 23 at 14:49
  • Is TodayPage your root view, you don't happen to have a parent view with a NavigationStack as well because that is not recommended. – Joakim Danielson Commented Mar 23 at 14:54
  • Does the sheet actually attempt to open twice because of this? Have you tried moving the .sheet modifier to the List instead of the NavigationStack? Without reproducible code, we cannot test any of this ourselves. – Andrei G. Commented Mar 24 at 0:25
  • I'll share another reproducible codes soon. Thanks for the replies. – joeydev Commented Mar 24 at 1:08
Add a comment  | 

1 Answer 1

Reset to default 0

The 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.

发布评论

评论列表(0)

  1. 暂无评论