Skip to content

Fix launch crash after SwiftData schema upgrade - #28

Draft
gsbernstein wants to merge 1 commit into
masterfrom
cursor/swiftdata-store-recovery-999a
Draft

Fix launch crash after SwiftData schema upgrade#28
gsbernstein wants to merge 1 commit into
masterfrom
cursor/swiftdata-store-recovery-999a

Conversation

@gsbernstein

Copy link
Copy Markdown
Owner

Problem

Users upgrading from a build with the old UserPreferences schema (maxSleepHoursPerNight / minSleepHoursPerNight) to the current schema (earliestReasonableBedtime) crash on launch. SwiftData cannot lightweight-migrate that change, and BedtimeApp currently calls fatalError when ModelContainer creation fails.

This matches a TestFlight report where the app crashes repeatedly on first open after updating (HealthKit/Garmin sync is unrelated — the crash happens during SwiftData init, before any HealthKit code runs).

Fix

Port the store-reset recovery that already exists on cursor/sleep-bank-insights-023c (70fb413) but was never merged to master:

  1. Try to open the existing store
  2. On failure, delete the store file plus -shm/-wal sidecars
  3. Retry container creation

UserPreferences only holds settings (sleep goal, wake time, etc.) with sensible defaults, so resetting the store is an acceptable tradeoff vs. a hard crash. Source exclusions and other prefs stored in UserDefaults are unaffected.

Verification

On macOS/Xcode, install a build with the old schema, set custom preferences, then upgrade to this build and confirm the app launches (settings reset to defaults) instead of crashing.

Open in Web Open in Cursor 

When UserPreferences schema changes can't be lightweight-migrated (e.g.
maxSleepHoursPerNight removal in #20), delete the stale store files and
recreate the container instead of fatalError on launch.

Co-authored-by: Greg <gsbernstein@users.noreply.github.com>
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