UserDefaults: a wolf in sheep's clothing
summary
UserDefaults can silently return nil while the device is locked or during pre-warmed launches, allowing that temporary nil to overwrite real data; a reliable repro is live activity + reboot + open, which fails every time. Investigation uses file protection, checking isProtectedDataAvailable, switching to file-backed storage in an app group with atomic writes and a dispatch queue, coordinating cross-process access with NSFileCoordinator, caching in memory and observing disk changes. A Swift Codable + property‑wrapper library is built and published and it works to avoid the data-loss class.
AI-written summary · about
chapters (10)
- 0:00Introduction
- 3:09UserDefaults data loss bug
- 8:01UserDefaults fails on locked devices
- 11:34Pre-warming risks
- 13:41Impact of UserDefaults data loss
- 15:44Workarounds for locked devices
- 17:30Safer alternative to UserDefaults
- 24:11Modern Swift storage design
- 30:12SwiftUI with custom storage
- 32:40Key takeaways








