mirror of
https://github.com/DefinedNet/mobile_nebula.git
synced 2025-02-15 08:15:27 +00:00
Use an AsyncStream of site updates to avoid concurrency warnings
This commit is contained in:
parent
275b4a50fb
commit
e4940d3e3a
2 changed files with 20 additions and 7 deletions
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
||||
|
|
Loading…
Reference in a new issue