项目开启了 -default-isolation=MainActor upcoming feature,导致:
1. static let shared 默认被视为 MainActor 隔离,即使 class 标了
@unchecked Sendable,从其他 actor(如 AIRuntime)同步访问仍报
"Expression is 'async' but is not marked with 'await'".
修法:ModelStore.shared 和 FileVault.shared 都加 nonisolated(unsafe)
修饰,明确"任何隔离上下文都可同步访问"。
2. AIRuntime.generate() 内的 Task { ... } 继承 AIRuntime actor 隔离,
self.recordRate 是同 actor 内部调用,不需要 await,否则报
"No 'async' operations occur within 'await' expression".
修法:去掉冗余的 await。
** BUILD SUCCEEDED ** 已验证。
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
102 lines
3.1 KiB
Swift
102 lines
3.1 KiB
Swift
import Foundation
|
|
import UIKit
|
|
|
|
enum FileVaultError: Error {
|
|
case readFailed
|
|
case writeFailed
|
|
case removeFailed
|
|
case decodeFailed
|
|
}
|
|
|
|
/// `@unchecked Sendable`:rootURL 是 let,方法只 I/O 到沙盒目录(线程安全),
|
|
/// 可被任意 actor / Task 跨边界访问。
|
|
/// `nonisolated(unsafe) shared`:见 ModelStore 同款注释。
|
|
final class FileVault: @unchecked Sendable {
|
|
nonisolated(unsafe) static let shared: FileVault = {
|
|
do {
|
|
let appSupport = try FileManager.default.url(
|
|
for: .applicationSupportDirectory,
|
|
in: .userDomainMask,
|
|
appropriateFor: nil,
|
|
create: true
|
|
)
|
|
let vaultURL = appSupport.appendingPathComponent("Vault", isDirectory: true)
|
|
return try FileVault(rootURL: vaultURL)
|
|
} catch {
|
|
fatalError("FileVault.shared init failed: \(error)")
|
|
}
|
|
}()
|
|
|
|
let rootURL: URL
|
|
|
|
init(rootURL: URL) throws {
|
|
self.rootURL = rootURL
|
|
try FileManager.default.createDirectory(
|
|
at: rootURL,
|
|
withIntermediateDirectories: true,
|
|
attributes: [.protectionKey: FileProtectionType.complete]
|
|
)
|
|
}
|
|
|
|
struct SavedAsset {
|
|
let relativePath: String
|
|
let bytes: Int
|
|
}
|
|
|
|
// MARK: - Path Safety
|
|
|
|
private func resolveSafePath(_ relativePath: String) throws -> URL {
|
|
guard !relativePath.contains("/"),
|
|
!relativePath.contains(".."),
|
|
!relativePath.isEmpty else {
|
|
throw FileVaultError.readFailed
|
|
}
|
|
let url = rootURL.appendingPathComponent(relativePath)
|
|
guard url.path.hasPrefix(rootURL.path) else {
|
|
throw FileVaultError.readFailed
|
|
}
|
|
return url
|
|
}
|
|
|
|
// MARK: - Public API
|
|
|
|
func writeJPEG(_ image: UIImage, quality: CGFloat = 0.85) throws -> SavedAsset {
|
|
guard let data = image.jpegData(compressionQuality: quality) else {
|
|
throw FileVaultError.writeFailed
|
|
}
|
|
let filename = "\(UUID().uuidString).jpg"
|
|
let url = rootURL.appendingPathComponent(filename)
|
|
try data.write(to: url, options: [.atomic, .completeFileProtection])
|
|
return SavedAsset(relativePath: filename, bytes: data.count)
|
|
}
|
|
|
|
func loadImage(relativePath: String) throws -> UIImage {
|
|
let url = try resolveSafePath(relativePath)
|
|
let data: Data
|
|
do {
|
|
data = try Data(contentsOf: url)
|
|
} catch {
|
|
throw FileVaultError.readFailed
|
|
}
|
|
guard let image = UIImage(data: data) else { throw FileVaultError.decodeFailed }
|
|
return image
|
|
}
|
|
|
|
func remove(relativePath: String) throws {
|
|
let url = try resolveSafePath(relativePath)
|
|
do {
|
|
try FileManager.default.removeItem(at: url)
|
|
} catch {
|
|
throw FileVaultError.removeFailed
|
|
}
|
|
}
|
|
|
|
func wipe() throws {
|
|
let fm = FileManager.default
|
|
let contents = try fm.contentsOfDirectory(at: rootURL, includingPropertiesForKeys: nil)
|
|
for url in contents {
|
|
try fm.removeItem(at: url)
|
|
}
|
|
}
|
|
}
|