From b8ef9c90a251df616c3e00253af71e9f3cd21688 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 9 Apr 2026 05:08:45 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20ObservableObject?= =?UTF-8?q?=20published=20array=20updates?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactored `@Published` array mutations in `CacheoutViewModel` to use `.map` instead of `for` loops. Modifying a `@Published` array in a loop triggers multiple `objectWillChange` notifications (one per iteration), causing unnecessary SwiftUI view re-renders. Co-authored-by: acebytes <2820910+acebytes@users.noreply.github.com> --- .jules/bolt.md | 3 ++ .../ViewModels/CacheoutViewModel.swift | 40 +++++++++++++++---- 2 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..cd9f700 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## YYYY-MM-DD - [Title] +**Learning:** In SwiftUI `ObservableObject` view models, mutating individual elements of a `@Published` array property inside a loop triggers a UI update notification for every change. For collections of value types (structs), utilize functional methods like `map` to batch updates into a single property assignment, significantly reducing unnecessary UI recalculations and improving responsiveness. +**Action:** Replace `for i in array.indices { array[i].prop = val }` with `array = array.map { var copy = $0; copy.prop = val; return copy }` to avoid multiple `objectWillChange` triggers. diff --git a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift index 13a9811..fc35b21 100644 --- a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift +++ b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift @@ -172,14 +172,23 @@ class CacheoutViewModel: ObservableObject { } func selectAllSafe() { - for i in scanResults.indices where scanResults[i].category.riskLevel == .safe && !scanResults[i].isEmpty { - scanResults[i].isSelected = true + // ⚡ Bolt Optimization: Using `.map` batches updates to a single `objectWillChange` trigger + // instead of O(N) triggers caused by looping over indices of a @Published array. + scanResults = scanResults.map { result in + var modified = result + if result.category.riskLevel == .safe && !result.isEmpty { + modified.isSelected = true + } + return modified } } func deselectAll() { - for i in scanResults.indices { - scanResults[i].isSelected = false + // ⚡ Bolt Optimization: Batch update single UI render + scanResults = scanResults.map { result in + var modified = result + modified.isSelected = false + return modified } deselectAllNodeModules() } @@ -193,17 +202,32 @@ class CacheoutViewModel: ObservableObject { } func selectStaleNodeModules() { - for i in nodeModulesItems.indices where nodeModulesItems[i].isStale { - nodeModulesItems[i].isSelected = true + // ⚡ Bolt Optimization: Batch update single UI render + nodeModulesItems = nodeModulesItems.map { item in + var modified = item + if item.isStale { + modified.isSelected = true + } + return modified } } func selectAllNodeModules() { - for i in nodeModulesItems.indices { nodeModulesItems[i].isSelected = true } + // ⚡ Bolt Optimization: Batch update single UI render + nodeModulesItems = nodeModulesItems.map { item in + var modified = item + modified.isSelected = true + return modified + } } func deselectAllNodeModules() { - for i in nodeModulesItems.indices { nodeModulesItems[i].isSelected = false } + // ⚡ Bolt Optimization: Batch update single UI render + nodeModulesItems = nodeModulesItems.map { item in + var modified = item + modified.isSelected = false + return modified + } } /// Menu bar label: show free GB in the tray