13.11 · Keychain status codes, detecting corruption, an LRU cache and library trade-offs

What happens when storage goes wrong or is shared: how you detect damage, how the Keychain reports state, how a cache evicts, and which engine to pick.

Mental model. Storage code is mostly error handling: handle each status, verify what you read, and decide who owns the file.

Verifying and caching

Keychain statuses and upsert, integrity checks and an LRU cache.

  1. Which OSStatus values matter when you use SecItem functions, and how do you write a correct save (upsert) of a secret? 13.11.1 · L3
    The Keychain API returns an OSStatus for everything, and the useful values are few. errSecSuccess (0); errSecDuplicateItem (−25299) when SecItemAdd finds an…
  2. How do you detect a damaged database or file, and what do you do about it? 13.11.2 · L3
    Corruption is rare but real (a full disk, a bad write, a failing storage controller, a copy of a WAL database without its log, a bug). Detect it at three…
  3. Implement an LRU cache with O(1) get and set: which data structures, and what does the eviction order look like? 13.11.3 · L3
    A least-recently-used cache needs two operations in constant time: find an entry by key, and move an entry to the “most recent” position or remove the “least…

Choosing

Core Data versus SwiftData versus GRDB versus Realm.

  1. Core Data, SwiftData, GRDB, SQLite.swift or Realm: how do you choose, and what do you give up with each? 13.11.4 · L5
    Choose by what you need to control. Core Data is the mature object-graph framework: lazy loading, change tracking, undo, migrations, CloudKit mirroring…

More in Persistence & data