Skip to content

Fix launch crash, infinite spinner, and HealthKit data loading - #29

Closed
gsbernstein wants to merge 2 commits into
masterfrom
cursor/fix-launch-crash-999a
Closed

Fix launch crash, infinite spinner, and HealthKit data loading#29
gsbernstein wants to merge 2 commits into
masterfrom
cursor/fix-launch-crash-999a

Conversation

@gsbernstein

@gsbernstein gsbernstein commented Jul 23, 2026

Copy link
Copy Markdown
Owner

Closed as superseded. Two of the three fixes here are now on master: the SwiftData store-reset recovery, and awaiting HealthKit queries (done properly in #40 via HKSampleQueryDescriptor). The remaining diagnostics and Grant Access work moved to #30.

One item was not carried forward: ContentView still inserts UserPreferences into the model context from inside a computed property read during body evaluation. Master's ContentView has since been restructured substantially, so reapplying that refactor belongs in its own PR against current master rather than in this stale branch.

Problem

TestFlight crash at launch (BedtimeApp.swift:23), plus an infinite loading spinner after reinstall, plus sleep data not appearing.

Root causes

  1. SwiftData store incompatible after Replace max sleep hours with earliest reasonable bedtime #20ModelContainer creation failed and hit fatalError before any UI loaded.
  2. Cancelled .task left HealthKit in .loading — seeding preferences and fetching HealthKit data shared one .task on a view whose identity changed when @Query populated, so SwiftUI cancelled it mid-flight and the permission state never advanced.
  3. HealthKit queries were fire-and-forgetloadSleepData() returned before HKSampleQuery callbacks ran, so the UI could leave the loading state before data arrived.
Open in Web Open in Cursor 

cursoragent and others added 2 commits July 23, 2026 18:20
- Stop inserting UserPreferences during view body evaluation; seed defaults
  in a task and gate the main UI on the persisted @query result instead.
- Recover from incompatible SwiftData stores by deleting stale files and
  retrying container creation (covers upgrades where delete/reinstall may
  still restore an old store via iCloud backup).
- Guard LastNightCard against an empty session array.

Co-authored-by: Greg <gsbernstein@users.noreply.github.com>
- Start the initial HealthKit fetch from HealthKitManager.init so it is not
  cancelled when ContentView switches from the preferences seeding state to
  the main UI (which left permissionsRequestState stuck at .loading).
- Await HKSampleQuery results before returning from fetchSleepData so sleep
  data is populated before the UI leaves the loading state.
- Add resumeLoadingIfNeeded() as a safety net on the main content view.

Co-authored-by: Greg <gsbernstein@users.noreply.github.com>
@cursor cursor Bot changed the title Fix launch crash on fresh install and schema upgrade Fix launch crash, infinite spinner, and HealthKit data loading Jul 23, 2026
@gsbernstein gsbernstein closed this Aug 7, 2026
@cursor
cursor Bot deleted the cursor/fix-launch-crash-999a branch August 7, 2026 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants