Persistence for Swift and SwiftUIs
Persistence in Swift apps means keeping data after the app closes. Options include UserDefaults for small settings, files in the app sandbox, the Keychain for secrets, and SwiftData or Core Data for structured models. Choosing among them depends on the size, sensitivity and relationships of the data.
Stage: Data and Concurrency · Part of the Swift and SwiftUI roadmap · Last reviewed 19 November 2026
What to learn in Persistence
- Save simple preferences with UserDefaults or @AppStorage
- Define a SwiftData model and query it from a view
- Insert, update and delete records through the model context
- Store tokens and passwords in the Keychain, not UserDefaults
- Read and write files in the documents directory
- Plan a lightweight migration when the model changes
Persistence interview questions
When should you use SwiftData instead of UserDefaults?
UserDefaults suits small values such as settings and flags. SwiftData is for structured, queryable records with relationships, like notes or workouts, where you need sorting, filtering and efficient storage of many items.
Why shouldn't passwords be stored in UserDefaults?
UserDefaults is stored as an unencrypted property list in the app container, so its contents can be read from a backup or a compromised device. The Keychain stores secrets encrypted and protected by system access rules.
Where Persistence fits in the Swift and SwiftUI roadmap
Persistence belongs to the Data and Concurrency stage (intermediate level), which takes about 21 hours. Real app behaviour.
Other units in this stage
Back to the full Swift and SwiftUI roadmap