When to Use UserDefaults

The interviewer asks

“When should you use UserDefaults versus Core Data or Keychain?”

How to answer

Pick storage by what the data is. Small, non-sensitive preferences go in UserDefaults. Secrets go in the Keychain. Structured data you query, relate or grow over time goes in a database: SwiftData for new apps, Core Data in existing ones, SQLite/GRDB when you need SQL. Large blobs and documents go in files with the right directory and protection class. The question is really testing whether you know each store's security, size and query trade-offs, so answer with a rule per category and one example each.

Key points

  • UserDefaults: settings, flags, small values; plain plist, loaded into memory, backed up, a required-reason API
  • Keychain: tokens, passwords, keys; encrypted, per-item accessibility, can be shared with extensions
  • SwiftData / Core Data: entities, relationships, predicates, migrations, iCloud sync; SwiftData is the default for new SwiftUI apps
  • Files: images, downloads, documents, exports; Documents vs Application Support vs Caches, plus Data Protection
  • Memory caches (NSCache) for re-creatable data that should disappear under memory pressure

If they dig deeper

Why not "just use UserDefaults" for a list of items? There's no querying, no partial loading, no migrations, no relationships, and every write rewrites the domain while the whole domain sits in memory. Why not Core Data for an auth token? It isn't encrypted beyond Data Protection, it lands in device backups, and it syncs wherever the store syncs. Several stores often cooperate: a SwiftData store for content, the Keychain for the session token, UserDefaults for "last opened tab", Caches for thumbnails. App Groups let a widget share any of them.

Common pitfalls

Tokens in UserDefaults. Arrays of thousands of encoded objects in UserDefaults. Large images in Core Data attributes without Allows External Storage. Caches in Documents. Treating the Keychain as a general database. Choosing Realm for new work without considering its maintenance status.

How to use it

Use caseStorageWhy
Theme, onboarding flag, last tabUserDefaults / @AppStorageTiny, non-sensitive, read at launch
Value shared with a widgetUserDefaults(suiteName:) App Group suiteSame API, shared container
Preferences synced across the user's devicesNSUbiquitousKeyValueStoreiCloud key-value, small quota
Auth token, password, private keyKeychain (…ThisDeviceOnly)Encrypted, access-controlled
Notes, orders, anything listed or searched (new app)SwiftData (see SwiftData and SwiftUI)Queries, relationships, migrations, CloudKit
Same, existing codebase or batch operationsCore DataMature, NSPersistentCloudKitContainer
SQL-heavy, cross-platform schemaSQLite via GRDBFull SQL control
Downloads, images, documentsFiles (Application Support / Documents)Large, binary; Data Protection
Re-creatable thumbnails, responsesCaches directory + NSCachePurgeable, not backed up

Best practices

  • Prefer UserDefaults only for small, non-sensitive, non-queryable values
  • Prefer SwiftData for new structured data and Core Data where it already exists
  • Prefer the Keychain for anything that grants access to an account
  • Declare required-reason APIs (UserDefaults, file timestamps, disk space) in the privacy manifest
  • Avoid one store for everything; combine them deliberately

Related concepts

  • UserDefaults, Keychain, Core Data, File Storage, SQLite on iOS, SwiftData and SwiftUI