LocalData/Sources/LocalData/Documentation.docc/GettingStarted.md
Matt Bruce 48546bdf62 Update Documentation.docc, Services and docs
Summary:
- Sources: update Documentation.docc, Services
- Docs: update docs for README

Stats:
- 4 files changed, 15 insertions(+), 2 deletions(-)
2026-01-18 13:43:11 -06:00

1.5 KiB

Getting Started

Follow this guide to define keys, configure LocalData, and register catalogs for auditing.

1) Define keys

Keys are static, typed constants. Do not use dynamic key names.

import LocalData

extension StorageKey where Value == String {
    static let userToken = StorageKey(
        name: "user_token",
        domain: .keychain(service: "com.myapp"),
        owner: "Auth",
        description: "Current user auth token."
    )
}

2) Configure the router (optional)

Set defaults once at app launch to avoid repeating service identifiers.

let storageConfig = StorageConfiguration(
    defaultKeychainService: "com.myapp.keychain",
    defaultAppGroupIdentifier: "group.com.myapp"
)
await StorageRouter.shared.updateStorageConfiguration(storageConfig)

3) Register catalogs

Catalogs are mandatory if you want auditing and duplicate detection.

Important

Once any catalog is registered, all storage operations require the key to be present in a registered catalog. Unregistered keys will throw StorageError.unregisteredKey.

struct AuthCatalog: StorageKeyCatalog {
    let allKeys: [AnyStorageKey] = [
        .key(StorageKey.userToken)
    ]
}

try await StorageRouter.shared.registerCatalog(AuthCatalog())

4) Store and read values

try await StorageRouter.shared.set("token", for: .userToken)
let token = try await StorageRouter.shared.get(.userToken)

Next steps