implemented a 3 seconds throttling of LLS updates.

This commit is contained in:
Mauro Romito
2026-04-20 16:27:51 +02:00
committed by Mauro
parent d49a6e5a5d
commit 22e3c81da1
@@ -22,6 +22,9 @@ class LiveLocationManager: NSObject, LiveLocationManagerProtocol, CLLocationMana
@CancellableTask @CancellableTask
private var locationUpdatesTask: Task<Void, Never>? private var locationUpdatesTask: Task<Void, Never>?
/// Subject used to pipe location updates through Combine's throttle operator.
private let locationUpdateSubject = PassthroughSubject<CLLocationUpdate, Never>()
private var cancellables = Set<AnyCancellable>() private var cancellables = Set<AnyCancellable>()
var authorizationStatus: CurrentValuePublisher<CLAuthorizationStatus, Never> { var authorizationStatus: CurrentValuePublisher<CLAuthorizationStatus, Never> {
@@ -125,6 +128,17 @@ class LiveLocationManager: NSObject, LiveLocationManagerProtocol, CLLocationMana
// MARK: - Private // MARK: - Private
private func setupSubscriptions() { 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 appSettings.$liveLocationSharingTimeoutDatesByRoomID
.removeDuplicates() .removeDuplicates()
.sink { [weak self] sessions in .sink { [weak self] sessions in
@@ -166,7 +180,7 @@ class LiveLocationManager: NSObject, LiveLocationManagerProtocol, CLLocationMana
for try await update in CLLocationUpdate.liveUpdates() { for try await update in CLLocationUpdate.liveUpdates() {
guard let self, !Task.isCancelled else { break } guard let self, !Task.isCancelled else { break }
await self.sendLocationToActiveRooms(update) self.locationUpdateSubject.send(update)
} }
} catch { } catch {
MXLog.error("Live location updates failed with error: \(error)") MXLog.error("Live location updates failed with error: \(error)")