From f2d3945ff3edebbd4531f0f69df3d66b03d12a66 Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Mon, 20 Apr 2026 16:27:51 +0200 Subject: [PATCH] implemented a 3 seconds throttling of LLS updates. --- .../Location/LiveLocationManager.swift | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ElementX/Sources/Services/Location/LiveLocationManager.swift b/ElementX/Sources/Services/Location/LiveLocationManager.swift index 7f6efaac4..530099aea 100644 --- a/ElementX/Sources/Services/Location/LiveLocationManager.swift +++ b/ElementX/Sources/Services/Location/LiveLocationManager.swift @@ -22,6 +22,9 @@ class LiveLocationManager: NSObject, LiveLocationManagerProtocol, CLLocationMana @CancellableTask private var locationUpdatesTask: Task? + /// Subject used to pipe location updates through Combine's throttle operator. + private let locationUpdateSubject = PassthroughSubject() + private var cancellables = Set() var authorizationStatus: CurrentValuePublisher { @@ -49,7 +52,7 @@ class LiveLocationManager: NSObject, LiveLocationManagerProtocol, CLLocationMana setupSubscriptions() } - + // MARK: - LiveLocationManagerProtocol @discardableResult @@ -125,6 +128,17 @@ class LiveLocationManager: NSObject, LiveLocationManagerProtocol, CLLocationMana // MARK: - Private private func setupSubscriptions() { + locationUpdateSubject + .throttle(for: .seconds(3), scheduler: DispatchQueue.main, latest: true) + .sink { [weak self] update in + guard let self else { return } + guard !appSettings.liveLocationSharingTimeoutDatesByRoomID.isEmpty else { return } + Task { [weak self] in + await self?.sendLocationToActiveRooms(update) + } + } + .store(in: &cancellables) + appSettings.$liveLocationSharingTimeoutDatesByRoomID .removeDuplicates() .sink { [weak self] sessions in @@ -166,7 +180,7 @@ class LiveLocationManager: NSObject, LiveLocationManagerProtocol, CLLocationMana for try await update in CLLocationUpdate.liveUpdates() { guard let self, !Task.isCancelled else { break } - await self.sendLocationToActiveRooms(update) + self.locationUpdateSubject.send(update) } } catch { MXLog.error("Live location updates failed with error: \(error)")