Make rig saves non-blocking (background disk I/O)

Save/delete/eraseAll now update the in-memory entries on the main actor
immediately and run the file write/delete — including NSFileCoordinator —
on a serial background queue, removing the UI stall the synchronous
coordinated write caused (especially on the iCloud path).

- Encoding stays synchronous so encode errors still surface to the caller;
  only the disk write is deferred (logs on failure).
- File-system helpers and coordinated I/O are now nonisolated static so the
  background queue can call them without touching actor state.
- Add flushPendingWrites() to drain the queue; called by tests asserting
  on-disk state and by both app shells at termination / background.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-27 19:40:02 -07:00
parent e8e134499d
commit 849240e6e2
5 changed files with 123 additions and 75 deletions
+10 -7
View File
@@ -60,13 +60,16 @@ still works; the sidebar shows the `icloud.slash` glyph.
### Known limitations / deferred (from pre-PR review)
- **Main-thread iCloud I/O.** `RigLibraryLocation.resolve()` does a blocking ubiquity lookup, and
`RigLibraryStore`'s `NSFileCoordinator` reads/writes run on the main actor. Mitigated for the
common case: `resolve()` short-circuits to local (no blocking call) when no iCloud account is
signed in, and coordinated writes only happen on explicit, occasional user saves. A full fix
needs an async resolve + a loading state in the builder, and must move seeding *after* the iCloud
state is known — otherwise a first-launch seed races the synced library and creates a spurious
"Default" rig on every device. Deferred deliberately.
- **Background writes.** Save/delete/erase update the in-memory `entries` on the main actor
instantly (UI never blocks) and run the file write/delete — including `NSFileCoordinator` — on a
serial background queue (`ioQueue`). `flushPendingWrites()` drains it (used by tests and at app
termination / iOS background). This removed the save-time UI stall.
- **Remaining main-thread I/O.** `RigLibraryLocation.resolve()` still does a blocking ubiquity
lookup (short-circuited to local when no iCloud account is signed in), and the *reads* in
`reloadFromDirectory()` (launch + iCloud metadata updates) are still synchronous on the main
actor. A full fix needs an async resolve + a loading state in the builder, and must move seeding
*after* the iCloud state is known — otherwise a first-launch seed races the synced library and
creates a spurious "Default" rig on every device. Deferred deliberately.
- **`eraseAll()`** clears the in-memory list even if a file deletion fails (it still throws the
first error). A re-`start()` re-scans and re-surfaces any undeleted rigs, so state self-heals.
- Possible later work: conflict UI for simultaneous multi-device edits; the async-resolve redesign