From 325f8fa5e93937eae3c517427990b73f6425569c Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 31 Mar 2026 03:52:55 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Offload=20blocking=20I/O=20?= =?UTF-8?q?to=20background=20tasks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: acebytes <2820910+acebytes@users.noreply.github.com> --- .jules/bolt.md | 3 +++ .../Cacheout/ViewModels/CacheoutViewModel.swift | 14 ++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..e50daaa --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-24 - Offloading Blocking Tasks from @MainActor +**Learning:** In Swift, synchronous, blocking operations like `Foundation.Process.waitUntilExit()`, `readDataToEndOfFile()`, and `DiskInfo.current()` will freeze the UI if executed within an `@MainActor` context (like a ViewModel). +**Action:** Use `await Task.detached { ... }.value` to offload these blocking operations to a background thread, preventing UI unresponsiveness and adhering to structured concurrency best practices. \ No newline at end of file diff --git a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift index 13a9811..39b68e8 100644 --- a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift +++ b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift @@ -147,7 +147,7 @@ class CacheoutViewModel: ObservableObject { func scan() async { isScanning = true isNodeModulesScanning = true - diskInfo = DiskInfo.current() + diskInfo = await Task.detached { DiskInfo.current() }.value // Scan caches and node_modules in parallel async let cacheResults = scanner.scanAll(CacheCategory.allCategories) @@ -241,10 +241,12 @@ class CacheoutViewModel: ObservableObject { ] do { - try process.run() - process.waitUntilExit() - let data = pipe.fileHandleForReading.readDataToEndOfFile() - let output = String(data: data, encoding: .utf8) ?? "" + let output: String = try await Task.detached { + try process.run() + process.waitUntilExit() + let data = pipe.fileHandleForReading.readDataToEndOfFile() + return String(data: data, encoding: .utf8) ?? "" + }.value if process.terminationStatus == 0 { // Extract "Total reclaimed space:" line @@ -270,7 +272,7 @@ class CacheoutViewModel: ObservableObject { } // Refresh disk info after prune - diskInfo = DiskInfo.current() + diskInfo = await Task.detached { DiskInfo.current() }.value } func clean() async {