소스 정보
- 저장소
- xu-xiang/everything-claude-code-zh
- 최근 소스 활동
- 2026년 3월 5일 01:40
- 감지된 SKILL.md 언어
- 중국어
- 스타
- 1,867
- 포크
- 306
설치 방법
기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.
소스 파일 검토
설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.
메뉴
기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.
설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.
SOC 직업 분류 기준
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
직접 명령은 검토 Prompt를 거치지 않습니다. 실행하기 전에 소스를 확인하세요.
npx skills add https://github.com/xu-xiang/everything-claude-code-zh --skill swift-actor-persistence명령은 한 줄로 유지됩니다. 복사하기 전에 가로로 스크롤해 전체 내용을 확인하세요.
로컬 사본을 원하시나요? SkillsMP에서 현재 제공할 수 있는 파일을 다운로드하세요.
SKILL.md 표시 중
| name | swift-actor-persistence |
| description | Swift 中使用 actor 实现的线程安全数据持久化 —— 结合内存缓存与文件存储,从设计上消除数据竞争。 |
| origin | ECC |
使用 Swift actor 构建线程安全数据持久化层的模式。该模式结合了内存缓存(In-memory caching)与基于文件的存储,利用 actor 模型(Actor model)在编译时消除数据竞争(Data races)。
Actor 模型保证了序列化访问(Serialized access)—— 由编译器强制执行,确保不会出现数据竞争。
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)
// 在 init 期间同步加载(此时 actor 隔离尚未生效)
self.cache = Self.loadSynchronously(from: fileURL)
}
// MARK: - 公共 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 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 {
return [:]
}
return Dictionary(uniqueKeysWithValues: items.map { ($0.id, $0) })
}
}
由于 actor 隔离(Actor isolation),所有调用都会自动变为异步:
let repository = LocalRepository<Question>()
// 读取 —— 从内存缓存中进行快速 O(1) 查找
let question = await repository.find(by: "q-001")
let allQuestions = await repository.loadAll()
// 写入 —— 原子化地更新缓存并持久化到文件
try await repository.save(newQuestion)
try await repository.delete("q-001")
@Observable
final class QuestionListViewModel {
private(set) var questions: [Question] = []
private let repository: LocalRepository<Question>
init(repository: LocalRepository<Question> = LocalRepository()) {
self.repository = repository
}
func load() async {
questions = await repository.loadAll()
}
func add(_ question: Question) async throws {
try await repository.save(question)
questions = await repository.loadAll()
}
}
| 决策 | 理由 |
|---|---|
| 使用 Actor (而非 class + lock) | 编译器强制执行线程安全,无需手动同步 |
| 内存缓存 + 文件持久化 | 缓存提供快速读取,磁盘提供持久化写入 |
| 在 init 中同步加载 | 避免异步初始化的复杂性,且对本地文件影响较小 |
| 以 ID 为键的字典 (Dictionary) | 通过标识符进行 O(1) 查找 |
泛型支持 Codable & Identifiable | 可在任何模型类型中复用 |
原子化文件写入 (.atomic) | 防止崩溃时出现部分写入导致的数据损坏 |
Sendable 类型.atomic 写入 以防止应用在写入过程中崩溃导致数据损坏init 中同步加载 —— 异步初始化会增加复杂性,而对本地文件的同步读取收益更高@Observable ViewModel 实现响应式 UI 更新DispatchQueue 或 NSLock 而非 actorawait 的 —— 调用者必须处理异步上下文nonisolated 绕过 actor 隔离(这违背了使用 actor 的初衷)DispatchQueue 的线程安全方案