[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M6G2PMSS5S9ZW0YM7X2BAP8D":3,"skill-same:01M6G2PMSS5S9ZW0YM7X2BAP8D":64},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":23,"publishedAt":35,"linkOnly":36,"markdownContent":37,"markdownBody":38,"frontMatter":39,"refName":41,"commitSha":42,"skillDirectory":43,"forksCount":44,"repositoryHtmlUrl":45,"repositoryHomepageUrl":6,"ownerHtmlUrl":46,"ownerBlogUrl":47,"ownerDescription":48,"updatedAt":49,"install":50,"resources":63},"01M6G2PMSS5S9ZW0YM7X2BAP8D","swift-actor-persistence",null,"Thread-safe data persistence in Swift using actors — in-memory cache with file-backed storage, eliminating data races by design.","oabdelmaksoud\u002FAgention","oabdelmaksoud","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F50699149?v=4","01M6D5CVVN778ZCDP1WHS816SP","Omar Abdelmaksoud","01M6D8A4TE76V3DCSH3D94VG1V",2,"MIT","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswift-actor-persistence\u002FSKILL.md",[18],{"categoryNo":19,"categoryName":20,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000015","视觉设计","SCENARIO","使用场景",[24,25,26,27,28,29,30,31,32,33,34],"swift","persistence","swift actor persistence","design","safe","memory","storage","file","cache","data","with","2026-11-10 10:12:30",false,"---\nname: swift-actor-persistence\ndescription: Thread-safe data persistence in Swift using actors — in-memory cache with file-backed storage, eliminating data races by design.\norigin: ECC\n---\n\n# Swift Actors for Thread-Safe Persistence\n\nPatterns 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.\n\n## When to Activate\n\n- Building a data persistence layer in Swift 5.5+\n- Need thread-safe access to shared mutable state\n- Want to eliminate manual synchronization (locks, DispatchQueues)\n- Building offline-first apps with local storage\n\n## Core Pattern\n\n### Actor-Based Repository\n\nThe actor model guarantees serialized access — no data races, enforced by the compiler.\n\n```swift\npublic actor LocalRepository\u003CT: Codable & Identifiable> where T.ID == String {\n    private var cache: [String: T] = [:]\n    private let fileURL: URL\n\n    public init(directory: URL = .documentsDirectory, filename: String = \"data.json\") {\n        self.fileURL = directory.appendingPathComponent(filename)\n        \u002F\u002F Synchronous load during init (actor isolation not yet active)\n        self.cache = Self.loadSynchronously(from: fileURL)\n    }\n\n    \u002F\u002F MARK: - Public API\n\n    public func save(_ item: T) throws {\n        cache[item.id] = item\n        try persistToFile()\n    }\n\n    public func delete(_ id: String) throws {\n        cache[id] = nil\n        try persistToFile()\n    }\n\n    public func find(by id: String) -> T? {\n        cache[id]\n    }\n\n    public func loadAll() -> [T] {\n        Array(cache.values)\n    }\n\n    \u002F\u002F MARK: - Private\n\n    private func persistToFile() throws {\n        let data = try JSONEncoder().encode(Array(cache.values))\n        try data.write(to: fileURL, options: .atomic)\n    }\n\n    private static func loadSynchronously(from url: URL) -> [String: T] {\n        guard let data = try? Data(contentsOf: url),\n              let items = try? JSONDecoder().decode([T].self, from: data) else {\n            return [:]\n        }\n        return Dictionary(uniqueKeysWithValues: items.map { ($0.id, $0) })\n    }\n}\n```\n\n### Usage\n\nAll calls are automatically async due to actor isolation:\n\n```swift\nlet repository = LocalRepository\u003CQuestion>()\n\n\u002F\u002F Read — fast O(1) lookup from in-memory cache\nlet question = await repository.find(by: \"q-001\")\nlet allQuestions = await repository.loadAll()\n\n\u002F\u002F Write — updates cache and persists to file atomically\ntry await repository.save(newQuestion)\ntry await repository.delete(\"q-001\")\n```\n\n### Combining with @Observable ViewModel\n\n```swift\n@Observable\nfinal class QuestionListViewModel {\n    private(set) var questions: [Question] = []\n    private let repository: LocalRepository\u003CQuestion>\n\n    init(repository: LocalRepository\u003CQuestion> = LocalRepository()) {\n        self.repository = repository\n    }\n\n    func load() async {\n        questions = await repository.loadAll()\n    }\n\n    func add(_ question: Question) async throws {\n        try await repository.save(question)\n        questions = await repository.loadAll()\n    }\n}\n```\n\n## Key Design Decisions\n\n| Decision | Rationale |\n|----------|-----------|\n| Actor (not class + lock) | Compiler-enforced thread safety, no manual synchronization |\n| In-memory cache + file persistence | Fast reads from cache, durable writes to disk |\n| Synchronous init loading | Avoids async initialization complexity |\n| Dictionary keyed by ID | O(1) lookups by identifier |\n| Generic over `Codable & Identifiable` | Reusable across any model type |\n| Atomic file writes (`.atomic`) | Prevents partial writes on crash |\n\n## Best Practices\n\n- **Use `Sendable` types** for all data crossing actor boundaries\n- **Keep the actor's public API minimal** — only expose domain operations, not persistence details\n- **Use `.atomic` writes** to prevent data corruption if the app crashes mid-write\n- **Load synchronously in `init`** — async initializers add complexity with minimal benefit for local files\n- **Combine with `@Observable`** ViewModels for reactive UI updates\n\n## Anti-Patterns to Avoid\n\n- Using `DispatchQueue` or `NSLock` instead of actors for new Swift concurrency code\n- Exposing the internal cache dictionary to external callers\n- Making the file URL configurable without validation\n- Forgetting that all actor method calls are `await` — callers must handle async context\n- Using `nonisolated` to bypass actor isolation (defeats the purpose)\n\n## When to Use\n\n- Local data storage in iOS\u002FmacOS apps (user data, settings, cached content)\n- Offline-first architectures that sync to a server later\n- Any shared mutable state that multiple parts of the app access concurrently\n- Replacing legacy `DispatchQueue`-based thread safety with modern Swift concurrency\n","# Swift Actors for Thread-Safe Persistence\n\nPatterns 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.\n\n## When to Activate\n\n- Building a data persistence layer in Swift 5.5+\n- Need thread-safe access to shared mutable state\n- Want to eliminate manual synchronization (locks, DispatchQueues)\n- Building offline-first apps with local storage\n\n## Core Pattern\n\n### Actor-Based Repository\n\nThe actor model guarantees serialized access — no data races, enforced by the compiler.\n\n```swift\npublic actor LocalRepository\u003CT: Codable & Identifiable> where T.ID == String {\n    private var cache: [String: T] = [:]\n    private let fileURL: URL\n\n    public init(directory: URL = .documentsDirectory, filename: String = \"data.json\") {\n        self.fileURL = directory.appendingPathComponent(filename)\n        \u002F\u002F Synchronous load during init (actor isolation not yet active)\n        self.cache = Self.loadSynchronously(from: fileURL)\n    }\n\n    \u002F\u002F MARK: - Public API\n\n    public func save(_ item: T) throws {\n        cache[item.id] = item\n        try persistToFile()\n    }\n\n    public func delete(_ id: String) throws {\n        cache[id] = nil\n        try persistToFile()\n    }\n\n    public func find(by id: String) -> T? {\n        cache[id]\n    }\n\n    public func loadAll() -> [T] {\n        Array(cache.values)\n    }\n\n    \u002F\u002F MARK: - Private\n\n    private func persistToFile() throws {\n        let data = try JSONEncoder().encode(Array(cache.values))\n        try data.write(to: fileURL, options: .atomic)\n    }\n\n    private static func loadSynchronously(from url: URL) -> [String: T] {\n        guard let data = try? Data(contentsOf: url),\n              let items = try? JSONDecoder().decode([T].self, from: data) else {\n            return [:]\n        }\n        return Dictionary(uniqueKeysWithValues: items.map { ($0.id, $0) })\n    }\n}\n```\n\n### Usage\n\nAll calls are automatically async due to actor isolation:\n\n```swift\nlet repository = LocalRepository\u003CQuestion>()\n\n\u002F\u002F Read — fast O(1) lookup from in-memory cache\nlet question = await repository.find(by: \"q-001\")\nlet allQuestions = await repository.loadAll()\n\n\u002F\u002F Write — updates cache and persists to file atomically\ntry await repository.save(newQuestion)\ntry await repository.delete(\"q-001\")\n```\n\n### Combining with @Observable ViewModel\n\n```swift\n@Observable\nfinal class QuestionListViewModel {\n    private(set) var questions: [Question] = []\n    private let repository: LocalRepository\u003CQuestion>\n\n    init(repository: LocalRepository\u003CQuestion> = LocalRepository()) {\n        self.repository = repository\n    }\n\n    func load() async {\n        questions = await repository.loadAll()\n    }\n\n    func add(_ question: Question) async throws {\n        try await repository.save(question)\n        questions = await repository.loadAll()\n    }\n}\n```\n\n## Key Design Decisions\n\n| Decision | Rationale |\n|----------|-----------|\n| Actor (not class + lock) | Compiler-enforced thread safety, no manual synchronization |\n| In-memory cache + file persistence | Fast reads from cache, durable writes to disk |\n| Synchronous init loading | Avoids async initialization complexity |\n| Dictionary keyed by ID | O(1) lookups by identifier |\n| Generic over `Codable & Identifiable` | Reusable across any model type |\n| Atomic file writes (`.atomic`) | Prevents partial writes on crash |\n\n## Best Practices\n\n- **Use `Sendable` types** for all data crossing actor boundaries\n- **Keep the actor's public API minimal** — only expose domain operations, not persistence details\n- **Use `.atomic` writes** to prevent data corruption if the app crashes mid-write\n- **Load synchronously in `init`** — async initializers add complexity with minimal benefit for local files\n- **Combine with `@Observable`** ViewModels for reactive UI updates\n\n## Anti-Patterns to Avoid\n\n- Using `DispatchQueue` or `NSLock` instead of actors for new Swift concurrency code\n- Exposing the internal cache dictionary to external callers\n- Making the file URL configurable without validation\n- Forgetting that all actor method calls are `await` — callers must handle async context\n- Using `nonisolated` to bypass actor isolation (defeats the purpose)\n\n## When to Use\n\n- Local data storage in iOS\u002FmacOS apps (user data, settings, cached content)\n- Offline-first architectures that sync to a server later\n- Any shared mutable state that multiple parts of the app access concurrently\n- Replacing legacy `DispatchQueue`-based thread safety with modern Swift concurrency\n",{"name":5,"origin":40,"description":7},"ECC","main","a318f48de3c5718e8ea2e9f6fae7790e8a759e85","upstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswift-actor-persistence",0,"https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud","https:\u002F\u002Fwww.linkedin.com\u002Fin\u002Fo-abdelmaksoud\u002F","Full-Stack Engineer | AI Systems Architect | Building autonomous multi-agent systems","2026-03-08 21:23:03",{"source":8,"skillName":5,"commands":51,"prompt":61,"zipAvailable":62},[52,55,58],{"tool":53,"command":54},"npx","npx skills add oabdelmaksoud\u002FAgention --skill swift-actor-persistence",{"tool":56,"command":57},"bunx","bunx skills add oabdelmaksoud\u002FAgention --skill swift-actor-persistence",{"tool":59,"command":60},"pnpm","pnpm dlx skills add oabdelmaksoud\u002FAgention --skill swift-actor-persistence","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswift-actor-persistence\u002FSKILL.md",true,[],[65,86,100,133,159,177],{"skillNo":66,"name":67,"nameZh":6,"description":68,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":69,"categories":70,"keywords":74,"publishedAt":35},"01M6G2QFCBNBYAJF2KTK85G9A6","visa-doc-translate","Translate visa application documents (images) to English and create a bilingual PDF with original and translation","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fvisa-doc-translate\u002FSKILL.md",[71],{"categoryNo":72,"categoryName":73,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000002","文档处理",[75,76,77,78,34,79,80,81,82,83,84,85],"translate","doc","visa doc translate","bilingual","english","application","PDF","translation","documents","create","images",{"skillNo":87,"name":88,"nameZh":6,"description":89,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":90,"categories":91,"keywords":92,"publishedAt":35},"01M6G2QB09TSK6BY4ME5X20EVQ","verification-loop","A comprehensive verification system for Claude Code sessions.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fverification-loop\u002FSKILL.md",[],[93,94,95,96,97,98,99],"verification","loop","verification loop","sessions","code","claude code sessions","system",{"skillNo":101,"name":102,"nameZh":6,"description":103,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":104,"categories":105,"keywords":117,"publishedAt":35},"01M6G2Q6V2F7G03VAAF27FXPJR","tdd-workflow","Use this skill when writing new features, fixing bugs, or refactoring code. Enforces test-driven development with 80%+ coverage including unit, integration, and E2E tests.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Ftdd-workflow\u002FSKILL.md",[106,109,112],{"categoryNo":107,"categoryName":108,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000008","测试与质量",{"categoryNo":110,"categoryName":111,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000023","Skill与智能体开发",{"categoryNo":113,"categoryName":114,"taxonomyType":115,"taxonomyTypeDesc":116},"01JBSEEDCAT000000000000030","开发工程师","PROFESSION","适用职业",[118,119,120,121,122,123,124,125,126,97,127,128,129,130,34,131,132],"workflow","tdd","tdd workflow","use","unit","coverage","refactoring","e2e","integration","development","e2e tests","tests","driven","test","writing",{"skillNo":134,"name":135,"nameZh":6,"description":136,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":137,"categories":138,"keywords":139,"publishedAt":35},"01M6G2Q359YJRWAR2PFCEQAXF3","swiftui-patterns","SwiftUI architecture patterns, state management with @Observable, view composition, navigation, performance optimization, and modern iOS\u002FmacOS UI best practices.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswiftui-patterns\u002FSKILL.md",[],[140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,34],"patterns","swiftui","swiftui patterns","architecture","macos","performance","state management","best practices","state","ui","Optimization","ios","architecture patterns","Composition","performance optimization","view","management","swiftui architecture","navigation",{"skillNo":160,"name":161,"nameZh":6,"description":162,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":163,"categories":164,"keywords":166,"publishedAt":35},"01M6G2PY8B2FWZTWPZ84H83Y7C","swift-protocol-di-testing","Protocol-based dependency injection for testable Swift code — mock file system, network, and external APIs using focused protocols and Swift Testing.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswift-protocol-di-testing\u002FSKILL.md",[165],{"categoryNo":107,"categoryName":108,"taxonomyType":21,"taxonomyTypeDesc":22},[167,168,24,169,97,170,31,171,172,99,173,174,175,176],"swift protocol di testing","protocol","testing","protocols","network","injection","dependency","swift testing","dependency injection","mock",{"skillNo":178,"name":179,"nameZh":6,"description":180,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":181,"categories":182,"keywords":183,"publishedAt":35},"01M6G2PSCN2GXV9FFZDRRRWVB6","swift-concurrency-6-2","Swift 6.2 Approachable Concurrency — single-threaded by default, @concurrent for explicit background offloading, isolated conformances for main actor types.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswift-concurrency-6-2\u002FSKILL.md",[],[184,185,186,24,187,41,188],"concurrency","swift concurrency","swift concurrency 6 2","default","types"]