Snapshot @Published properties before async suspension points to prevent SIGBUS from corrupted pointers due to concurrent access.
43 lines
1.2 KiB
Swift
43 lines
1.2 KiB
Swift
import Foundation
|
|
import GRDB
|
|
import Libbox
|
|
|
|
public extension Profile {
|
|
nonisolated func updateRemoteProfile() async throws {
|
|
if type != .remote {
|
|
return
|
|
}
|
|
let url = remoteURL
|
|
let remoteContent = try await HTTPClient.getStringAsync(url)
|
|
try await BlockingIO.run {
|
|
var error: NSError?
|
|
LibboxCheckConfig(remoteContent, &error)
|
|
if let error {
|
|
throw error
|
|
}
|
|
}
|
|
await MainActor.run {
|
|
lastUpdated = Date()
|
|
}
|
|
try await ProfileManager.update(self)
|
|
do {
|
|
let oldContent = try await readAsync()
|
|
if oldContent == remoteContent {
|
|
return
|
|
}
|
|
} catch {}
|
|
try await writeAsync(remoteContent)
|
|
try await onProfileUpdated()
|
|
}
|
|
|
|
nonisolated func onProfileUpdated() async throws {
|
|
if await SharedPreferences.selectedProfileID.get() == id {
|
|
if let profile = try? await ExtensionProfile.load() {
|
|
if await profile.status == .connected {
|
|
try await profile.reloadService()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|