I use SwiftUI for my Mac OS app. When I change selection (from Inbox to Today) of the List in SidebarView with mouse click didSet is called twice(Sidebar body is calculated twice too). If I change selection with arrow (down | up) didSet is called once. Is it bug? I am sorry for my english.
Logs:
- with mouse click:
willSet = Optional(MiniTodo.NavigationItem.inbox) 2021-04-18 00:36:20 +0000
didSet = Optional(MiniTodo.NavigationItem.inbox) 2021-04-18 00:36:20 +0000
willSet = Optional(MiniTodo.NavigationItem.inbox) 2021-04-18 00:36:20 +0000
didSet = Optional(MiniTodo.NavigationItem.today) 2021-04-18 00:36:20 +0000
- with arrow:
willSet = Optional(MiniTodo.NavigationItem.inbox) 2021-04-18 00:37:11 +0000
didSet = Optional(MiniTodo.NavigationItem.today) 2021-04-18 00:37:11 +0000
import SwiftUI
class UIState: ObservableObject {
@Published var navigationItem: NavigationItem? = .inbox {
didSet {
print("didSet = \(navigationItem) \(Date())")
}
willSet {
print("willSet = \(navigationItem) \(Date())")
}
}
}
struct MiniTodoApp: App {
let persistenceController = PersistenceController.shared
@StateObject var uiState: UIState = UIState()
var body: some Scene {
WindowGroup {
NavigationView {
SidebarView()
.environmentObject(uiState)
//ContentView()
}
.navigationViewStyle(DoubleColumnNavigationViewStyle())
.environment(\.managedObjectContext, persistenceController.container.viewContext)
}
}
}
struct SidebarView: View {
@Environment(\.managedObjectContext) private var viewContext
@EnvironmentObject var uiState: UIState
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Folder.name, ascending: true)],
animation: .default)
private var folders: FetchedResults<Folder>
var body: some View {
print("==== Sidebar View ====")
print(#function)
print("======================")
print("")
return List(selection: $uiState.navigationItem) {
Group {
Label("Inbox", systemImage: "tray")
.tag(NavigationItem.inbox)
Label("Today", systemImage: "sun.max.fill")
.listItemTint(.yellow)
.tag(NavigationItem.today)
Label("Plan", systemImage: "calendar")
.listItemTint(.red)
.tag(NavigationItem.plan)
Label("Completed", systemImage: "checkmark.square.fill")
.listItemTint(.green)
.tag(NavigationItem.completed)
}
Section(header:
HStack {
Text("Lists")
Spacer()
//if self.onHover {
Button {
withAnimation {
newFolder()
}
} label: {
Image(systemName: "plus.circle")
}
.buttonStyle(BorderlessButtonStyle())
.padding(EdgeInsets(top: 0.0, leading: 0.0, bottom: 0.0, trailing: 10.0))
// }
}
) {
ForEach(folders) { folder in
FolderRow(folder: folder)
.tag(NavigationItem.folder(folder: folder))
}
} // Section
.collapsible(false)
} // List
.listStyle(SidebarListStyle())
.toolbar {
ToolbarItem(placement: .navigation){
Button(action: toggleSidebar, label: {
Image(systemName: "sidebar.left")
})
}
}
} // body
// Toggle Sidebar Function
func toggleSidebar() {
NSApp.keyWindow?.firstResponder?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil)
}
func newFolder() {
withAnimation {
let newItem = Folder(context: viewContext)
newItem.name = "It is new folder"
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
}
You can use the
debounce
function on top of the navigationItem publisher and you will only receive thedidSet
once: