affaan-m avatar

Swift Actor Persistence

This is a practical pattern for local data persistence in Swift 5.5+ that uses actors instead of manual locks or DispatchQueues. You get an in-memory dictionary cache backed by JSON file storage, with compile-time guarantees against data races. The generic repository works with any Codable and Identifiable type, giving you O(1) lookups and atomic file writes. It's most useful for offline-first apps or anywhere you need thread-safe access to shared state without the ceremony of traditional synchronization primitives. The synchronous init load is a reasonable tradeoff that avoids async initialization complexity for local files.

skills.sh installs
5K
GitHub stars
229.1K
last updated
Jul 14, 2026

Install

$ npx -y skills add affaan-m/everything-claude-code --skill swift-actor-persistence --agent claude-code

Installs into .claude/skills of the current project.

Files

Swift Actors for Thread-Safe Persistence

Patterns for building thread-safe data persistence layers using Swift actors. Combines in-memory caching with file-backed storage, leveraging the actor model to eliminate data races at compile time.

When to Activate

  • Building a data persistence layer in Swift 5.5+
  • Need thread-safe access to shared mutable state
  • Want to eliminate manual synchronization (locks, DispatchQueues)
  • Building offline-first apps with local storage

Core Pattern

Actor-Based Repository

The actor model guarantees serialized access — no data races, enforced by the compiler.

public actor LocalRepository<T: Codable & Identifiable> where T.ID == String {
    private var cache: [String: T] = [:]
    private let fileURL: URL

    public init(directory: URL = .documentsDirectory, filename: String = "data.json") {
        self.fileURL = directory.appendingPathComponent(filename)
        // Synchronous load during init (actor isolation not yet active)
        self.cache = Self.loadSynchronously(from: fileURL)
    }

    // MARK: - Public API

Sponsors

Details

Repository
github.com/affaan-m/everything-claude-code
Category
Mobile Development

More from affaan-m/everything-claude-code

All 256
1affaan-m avatarswift-protocol-di-testingaffaan-m/everything-claude-codeswift protocol di testing5Kskills.sh installs
2affaan-m avatarswift-concurrency-6-2affaan-m/everything-claude-codeswift concurrency 6 24.9Kskills.sh installs
3forcedotcom avatarmobile-platform-native-capabilities-integrateforcedotcom/sf-skillsBuild a Salesforce LWC that uses native mobile device capabilities — barcode scanner, biometrics, location, NFC, calendar, contacts, document scanner, geofencin4.7Kskills.sh installs
4forcedotcom avatarmobile-platform-offline-validateforcedotcom/sf-skillsReview a Lightning Web Component for **mobile offline** compatibility — the Komaci offline static analyzer that pre-primes the data graph for Salesforce Mobile 4.7Kskills.sh installs
5affaan-m avatarflutter-dart-code-reviewaffaan-m/everything-claude-codeflutter dart code review4.7Kskills.sh installs
6affaan-m avatarkotlin-ktor-patternsaffaan-m/everything-claude-codekotlin ktor patterns4.7Kskills.sh installs