affaan-m avatar

Swift Actor Persistence

This gives you a generic actor-based repository pattern for thread-safe local persistence in Swift. It wraps an in-memory dictionary cache with file-backed JSON storage, so reads are fast and writes are durable. The actor isolation means no manual locks or dispatch queues, and the compiler enforces thread safety at build time. You get synchronous loading during init to avoid async initialization hell, atomic file writes to prevent corruption on crashes, and it works with any Codable type that has a string ID. Solid choice for offline-first iOS apps or anywhere you need shared mutable state without the usual concurrency footguns. The ViewModel integration example shows how it composes cleanly with SwiftUI's Observable macro.

skills.sh installs
5.4K
GitHub stars
300.3K
last updated
Aug 21, 2026

Install

$ npx -y skills add affaan-m/ecc --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/ecc
Categories
Mobile Development, Design & UI/UX
First seen
Jul 14, 2026

More from affaan-m/ecc

All 263
1affaan-m avatarcompose-multiplatform-patternsaffaan-m/everything-claude-codecompose multiplatform patterns5Kskills.sh installs
2dotnet avatarmaui-app-lifecycledotnet/skills.NET MAUI app lifecycle guidance — the four app states, cross-platform Window lifecycle events (Created, Activated, Deactivated, Stopped, Resumed, Destroying), 4Kskills.sh installs
3sickn33 avatarmobile-designsickn33/antigravity-awesome-skillsmobile design3Kskills.sh installs
4safaiyeh avatarapp-store-reviewsafaiyeh/app-store-review-skillapp store review1.6Kskills.sh installs
5capawesome-team avatarionic-expertcapawesome-team/skillsA comprehensive starting point for AI agents to work with the Ionic Framework. Covers core concepts, components, CLI, theming, layout, lifecycle, navigation, an1.6Kskills.sh installs
6mapbox avatarmapbox-ios-patternsmapbox/mapbox-agent-skillsOfficial integration patterns for Mapbox Maps SDK on iOS. Covers installation, adding markers, user location, custom data, styles, camera control, and featurese1.5Kskills.sh installs