Question 1
Choosing the right storage strategy
You’re building a feature with structured data, user settings, and large media files. How do you decide what goes in Core Data/SwiftData vs files vs Keychain?
Follow-ups
- What should never go in UserDefaults?
Answer outline
Split by data type and how you read/write it.
Core Data / SwiftData — relational app data, queries, relationships, and incremental updates.
UserDefaults — small app/user toggles and lightweight preferences.
File system — binary payloads (images, video, documents) and large blobs.
Keychain — secrets (tokens, keys, credentials).
Use file system for large media because disk I/O and lifecycle management are clearer — but pick the right directory: iOS gives you separate locations with different durability and eviction rules.
Temporary / cache-style storage — temporaryDirectory and Library/Caches: good for regenerable data (thumbnails, decoded buffers, downloads you can refetch). The OS may delete these under low storage; they are not your long-term contract with the user.
Durable app storage — Documents and Library/Application Support: use these for data the app must keep until the user deletes the app or you explicitly remove it (e.g. user-created exports, offline copies you cannot trivially re-download, bundled support assets). These participate in backup behavior differently than caches.
Define explicit cleanup for caches (TTL, max size, purge on logout) so growth stays predictable.
Principles
- Keychain for secrets only (auth tokens, refresh tokens, private keys).
- UserDefaults for small preferences only, not app databases or payload caches.
- Model metadata in DB, store heavy bytes in files, and link with stable IDs.