Use an AsyncStream of site updates to avoid concurrency warnings

This commit is contained in:
Caleb Jasik 2025-02-12 15:05:37 -06:00
parent 275b4a50fb
commit e4940d3e3a
No known key found for this signature in database
2 changed files with 20 additions and 7 deletions

View file

@ -25,15 +25,14 @@ func MissingArgumentError(message: String, details: Any?) -> FlutterError {
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
Task.detached {
await self.dnUpdater.updateAllLoop { [weak self] site in
self?.sites?.updateSite(site: site)
Task {
for await site in dnUpdater.siteUpdates {
self.sites?.updateSite(site: site)
// Send the refresh sites command on the main thread
DispatchQueue.main.async {
// Signal to the main screen to reload
self?.ui?.invokeMethod("refreshSites", arguments: nil)
self.ui?.invokeMethod("refreshSites", arguments: nil)
}
}
}

View file

@ -1,7 +1,7 @@
import Foundation
import os.log
actor DNUpdater {
class DNUpdater {
private let apiClient = APIClient()
private let timer = RepeatingTimer(timeInterval: 15 * 60) // 15 * 60 is 15 minutes
private let log = Logger(subsystem: "net.defined.mobileNebula", category: "DNUpdater")
@ -18,7 +18,7 @@ actor DNUpdater {
return
}
await self.updateSite(site: site, onUpdate: onUpdate)
self.updateSite(site: site, onUpdate: onUpdate)
}
}
@ -95,6 +95,20 @@ actor DNUpdater {
}
}
extension DNUpdater {
// Site updates provides an async/await alternative to `.updateAllLoop` that doesn't require a sendable closure.
// https://developer.apple.com/documentation/swift/asyncstream
var siteUpdates: AsyncStream<Site> {
AsyncStream { continuation in
self.updateAllLoop(onUpdate: { site in
continuation.yield(site)
})
}
}
}
// From https://medium.com/over-engineering/a-background-repeating-timer-in-swift-412cecfd2ef9
class RepeatingTimer {