13.1 · Where data belongs, and the Keychain

Persistence starts with a decision: what is this data, and what must happen to it when the app is deleted, restored or the device is lost? The wrong container is a bug even if the code is perfect.

Mental model. Pick the store by the data’s lifetime and sensitivity, not by what is convenient: secrets to the Keychain, user data to a database or files, rebuildable data to caches.

Choosing

The storage options and what survives what.

  1. UserDefaults, Keychain, files, SQLite, Core Data or SwiftData: how do you choose, and what survives deletion, reinstall and backup? 13.1.1 · L2
    Start with what the data is. Preferences and small flags (a theme, the last tab) go to UserDefaults (a property list in the app container; fast and simple, but…

Secrets

Keychain items, attributes and accessibility.

  1. How does the Keychain work, how do you add, read and update an item, and what do accessibility classes and access groups do? 13.1.2 · L3
    The Keychain is an encrypted database managed by the system, accessed through the C SecItem API with dictionaries of attributes. An item belongs to a class…

More in Persistence & data