Skip to content
Back to skills

Swift Actor Persistence

ASecurity

Use when thread-safe data persistence in Swift using actors — in-memory cache with file-backed storage, eliminating data races by design. Triggers on \"swift-actor-persistence\", \"swift actor persistence\", \"persistence\".

  • 2 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 19, 2026
ai-agentsswiftapi

Works with

  • api

Security analysis

A100/100

Scanned September 19, 2026

npx -y skills add majinmagros/magros.ai-skills --skill swift-actor-persistence --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Swift Actor Persistence?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Swift Actor Persistence
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/majinmagros-swift-actor-persistence/badge)](https://www.skillsdirectory.com/skills/majinmagros-swift-actor-persistence)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: swift-actor-persistence
description: "Use when thread-safe data persistence in Swift using actors — in-memory cache with file-backed storage, eliminating data races by design. Triggers on \"swift-actor-persistence\", \"swift actor persistence\", \"persistence\"."
metadata:
  origin: ECC
---

# 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.

```swift
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

    public func save(_ item: T) throws {
        cache[item.id] = item
        try persistToFile()
    }

    public func delete(_ id: String) throws {
        cache[id] = nil
        try persistToFile()
    }

    public func find(by id: String) -> T? {
        cache[id]
    }

    public func loadAll() -> [T] {
        Array(cache.values)
    }

    // MARK: - Private

    private func persistToFile() throws {
        let data = try JSONEncoder().encode(Array(cache.values))
        try data.write(to: fileURL, options: .atomic)
    }

    private static func loadSynchronously(from url: URL) -> [String: T] {
        guard let data = try? Data(contentsOf: url),
              let items = try? JSONDecoder().decode([T].self, from: data) else {

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…