widget-appgroup-snapshot-bridge
Home-screen widget goes blank or stale — recents blanked after an iCloud-sync launch ("No recent documents" while the app has plenty), pre-lock titles/thumbnails still visible on the widget while App-Lock is engaged, thumbnails broken after reinstall or device restore, or the widget silently shows nothing because containerURL(forSecurityApplicationGroupIdentifier:) returned nil. Use when building or debugging a WidgetKit extension that shares state with its host app via an App Group (snapshot file, reloadAllTimelines, shared UserDefaults suite).
- License 许可
- MIT
- 最近更新
- 2026-11-07
- Stars
- ★ 1
- Forks
- 1
01 前言 Front Matter
- name
- widget-appgroup-snapshot-bridge
- description
- Home-screen widget goes blank or stale — recents blanked after an iCloud-sync launch ("No recent documents" while the app has plenty), pre-lock titles/thumbnails still visible on the widget while App-Lock is engaged, thumbnails broken after reinstall or device restore, or the widget silently shows nothing because containerURL(forSecurityApplicationGroupIdentifier:) returned nil. Use when building or debugging a WidgetKit extension that shares state with its host app via an App Group (snapshot file, reloadAllTimelines, shared UserDefaults suite).
02 资源文件 1
- 参考文档references/snapshot-bridge.md3.8 KB
03 SKILL.md 正文
Widget ↔ App Bridge: One Codable Snapshot Over the App Group
Symptom
- Widget shows "No recent documents" after a launch even though the app has documents — classically the first launch after iCloud sync, when the local store is transiently empty and the app overwrites the snapshot with nothing.
- Widget still renders pre-lock content (titles, thumbnails) while App-Lock is engaged — a stale pre-redaction snapshot on disk.
- Thumbnails break after reinstall/restore — absolute file URLs were baked into the snapshot and the container path changed.
- Widget silently empty on one target: missing App Group entitlement makes
containerURL(forSecurityApplicationGroupIdentifier:)returnnilwith no error (surfaces only at altool validate). - A Lock Screen/StandBy widget renders blank while the device is locked — the snapshot file's protection class blocks the widget process from reading it (see the file-protection invariant under Fix).
Root cause
A widget runs in a separate process. Its only channels to the app are the
shared App Group container and WidgetCenter.shared.reloadAllTimelines().
Naive bridges fail in four repeatable ways: writing the snapshot on every model
change (so a transient-empty launch pass clobbers last-known-good); baking
absolute URLs (container paths differ per process/install); non-atomic writes
(widget reads a half-written file); and redaction decided ad hoc in two places
(so a stale pre-redaction snapshot leaks). Every app re-derives this
architecture — this skill is the canonical shape.
Fix
One Codable DTO compiled into both targets; app-side atomic writer;
widget-side pure reader; relative paths only; backfill on launch.
Invariants the implementation must hold:
- IDs are stable model IDs, never absolute URLs; thumbnail paths stay relative to the App Group container — absolute paths break after reinstall/restore because the container path changes per install.
- Invariant A (writer): never clobber a good snapshot with a
transient-empty one. On an iCloud-sync launch the local store is briefly
empty — if
documentsis empty but a snapshot with entries already exists on disk, skip the write and let the post-settle backfill publish real state. - Invariant B (writer + reader share one flag): the App-Lock redaction decision has exactly one source of truth. The writer mirrors App-Lock state into the shared UserDefaults suite; the reader re-checks that SAME flag on every load regardless of what's on disk, so a stale pre-redaction snapshot can never render. Neither side invents its own redaction signal — that's what causes double-redaction bugs.
- Writes are atomic (
.atomic, .completeFileProtectionUntilFirstUserAuthentication) so the widget process never reads a half-written file. Do not use.completeFileProtectionUnlessOpen: a Lock Screen/StandBy widget cannot open the file while the device is locked, so it renders blank while locked (see Symptom). The trade-off: the snapshot holds real titles and thumbnail paths on disk, protected only until the first unlock after boot. The writer does not redact the file; redaction is applied at render time by the reader (invariant B).
Read references/snapshot-bridge.md before implementing — the full
WidgetBridge shared DTO, WidgetSnapshotWriter (app-side, the only writer),
and WidgetSnapshotReader (widget-side, pure reader) implementing the
invariants above.
Completing the shape:
- Backfill on launch: after the store settles (post iCloud first-sync),
call
publishonce — heals missing, stale, or version-bumped snapshots:await store.awaitInitialLoad(); WidgetSnapshotWriter.publish(...). - Resolve relative paths at read time:
WidgetBridge.containerURL?.appendingPathComponent(entry.thumbnailRelativePath). - Version the filename (
-v1) so a DTO change reads as "no snapshot" (then backfill heals it) instead of a decode crash in the widget process. - Entitlement parity: every target that compiles
WidgetBridgemust carry the App Group entitlement, and targets that don't reference it must not — assert this in release pre-flight; the failure mode is silentnil.
Evidence
- a private document-scanner app —
WidgetBridge/WidgetSnapshotReadercommit series, incl.stop iCloud-sync launch from blanking widget recents(invariant A) and the App-Lock redaction mirroring fixes (invariant B). - a second private iOS app — independently re-derived the same App-Group snapshot architecture, motivating this canonical skill.
Related skills
biometric-applock— the App-Lock state this skill's invariant B mirrors into the shared suite and redacts against at render time.file-handoff-inbox-backstop— widget timeline launches re-trigger app cold starts; a poison share-inbox batch turns that into a boot loop.deep-link-resolver-applock-pathtraversal— widget entry taps deep-link into the app; route them through the single resolver (dropped, not deferred, while locked).swift6-mainactor-compile-fixes— the shared DTO must decode off-main in the widget process; mark the bridge/reader typesnonisolatedhonestly instead of@unchecked Sendableband-aids.swiftdata-inmemory-test-harness— drive the writer from an in-memory store to test the never-clobber and backfill paths.background-assets-manifest-drift-blind-redownload— another shared/cached resource whose identity must be verified by content, not presence, before reuse.diagnostic-export-error-redaction-boundary— a different redaction boundary (raw error descriptions) on the same app; compare when a feature needs more than one kind of redaction gate.
04 同仓库更多 Skills
second-wind
Orchestrate long unattended Claude Code runs across multiple repos with the wind CLI. Use when the user wants to run Claude Code overnight, resume sessions after the 5-hour usage limit, run Claude in many repos at once via tmux, or says "set up second wind", "wind up", "overnight run", "resume after limit", "usage limit orchestrator".
structured-output-adaptive-thinking-token-bloat
A Claude API call using output_config.format (json_schema structured outputs) with thinking:{type:"adaptive"} burns most of its output budget on thinking (observed ~21k of ~24k tokens) without improving JSON quality, making the run up to ~5x more expensive than the same call with thinking omitted — use when calling the Messages API with a json_schema output format for extraction, ranking, classification, or summarization tasks, or when a schema-constrained call's usage.output_tokens is far larger than the returned JSON.
review
Apply a code-review lens to a diff, branch, or file — find correctness bugs, security issues, and quality problems, ranked by severity, with a concrete fix for each. Use when the user says "review this", "review my diff", "check this PR", "look this over before I commit", or "is this safe". Skip when the user wants the code written, not reviewed.
refresh
Rebuild the prompt-craft command registry and usage profile, and print a summary of installed commands and your personalized recommendations. Use when you install or update plugins and want the advisor to pick them up immediately, or say "refresh prompt-craft", "rebuild the command registry".
refactor
Apply a refactoring lens — restructure code without changing behavior, guarded by tests that pass before and after. Use when the user says "refactor", "clean this up", "extract this", "simplify this", "reduce duplication", or "make this readable" on existing working code. Skip when the change is meant to alter behavior (that's a feature or a fix).
plan
Decompose a non-trivial task into an explicit, tracked plan with goals and per-step acceptance criteria before writing code. Use when the user says "plan this", "break this down", "what's the plan", or asks for a multi-step feature/refactor that touches more than one file. Pairs with plan mode. Skip for single-edit changes that need no decomposition.