From e4940d3e3aa0b33877f46497d478c9309e834ce3 Mon Sep 17 00:00:00 2001 From: Caleb Jasik Date: Wed, 12 Feb 2025 15:05:37 -0600 Subject: [PATCH] Use an AsyncStream of site updates to avoid concurrency warnings --- ios/Runner/AppDelegate.swift | 9 ++++----- ios/Runner/DNUpdate.swift | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift index 7ab4a9a..4323eb4 100644 --- a/ios/Runner/AppDelegate.swift +++ b/ios/Runner/AppDelegate.swift @@ -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) } - } } diff --git a/ios/Runner/DNUpdate.swift b/ios/Runner/DNUpdate.swift index 81a59c5..9c7a9c6 100644 --- a/ios/Runner/DNUpdate.swift +++ b/ios/Runner/DNUpdate.swift @@ -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 { + 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 {