Fix main-thread blocking I/O

This commit is contained in:
世界
2026-02-05 02:37:02 +08:00
parent 4ae421cd04
commit 2e682d746c
17 changed files with 643 additions and 237 deletions
@@ -16,14 +16,13 @@ final class ServiceLogViewModel: BaseViewModel {
}
nonisolated func loadContent() async {
var content = ""
do {
content = try String(contentsOf: FilePath.cacheDirectory.appendingPathComponent("stderr.log"))
} catch {}
if content.isEmpty {
do {
content = try String(contentsOf: FilePath.cacheDirectory.appendingPathComponent("stderr.log.old"))
} catch {}
let primaryLogURL = FilePath.cacheDirectory.appendingPathComponent("stderr.log")
let secondaryLogURL = FilePath.cacheDirectory.appendingPathComponent("stderr.log.old")
var content = await BlockingIO.run {
if let primaryContent = try? String(contentsOf: primaryLogURL), !primaryContent.isEmpty {
return primaryContent
}
return (try? String(contentsOf: secondaryLogURL)) ?? ""
}
#if DEBUG
if content.isEmpty {
@@ -53,8 +52,12 @@ final class ServiceLogViewModel: BaseViewModel {
}
nonisolated func deleteContent(dismiss: DismissAction) async {
try? FileManager.default.removeItem(at: FilePath.cacheDirectory.appendingPathComponent("stderr.log"))
try? FileManager.default.removeItem(at: FilePath.cacheDirectory.appendingPathComponent("stderr.log.old"))
let primaryLogURL = FilePath.cacheDirectory.appendingPathComponent("stderr.log")
let secondaryLogURL = FilePath.cacheDirectory.appendingPathComponent("stderr.log.old")
await BlockingIO.run {
try? FileManager.default.removeItem(at: primaryLogURL)
try? FileManager.default.removeItem(at: secondaryLogURL)
}
await MainActor.run {
dismiss()
isLoading = true
@@ -64,4 +67,11 @@ final class ServiceLogViewModel: BaseViewModel {
func generateShareFile() throws -> URL {
try content.generateShareFile(name: "service.log")
}
func generateShareFileAsync() async throws -> URL {
let content = content
return try await BlockingIO.run {
try content.generateShareFile(name: "service.log")
}
}
}