Modern Apps git

← Modern-Apps.git

findfamily-ios: stop background location when not sharing (#629)

Commit
5af395518ab12ef648547a459c8d6f7538eb4556
Author
Vayun Mathur <[email protected]>
When
2026-11-05 19:11 UTC (1 month ago)
Parents
1c838fb
Symptom: app kept using location in the background even with sharing
disabled for everyone, draining battery (iPhone 13 mini, v4.0).

Root cause: AppDelegate started the CoreLocation pipeline
unconditionally on every launch, and the 3s share/receive heartbeat
ran for the process lifetime. Neither consulted the sharing state,
so GPS plus network polling stayed alive with zero recipients.

Fix: gate location work on sharing state (any peer sendingEnabled,
pending auto-toggle timer, or live temporary link). Stopping tears
down live updates, background/service sessions, region/visit
monitoring, and the heartbeat; re-enabling sharing restarts them.
Hooked into DB change publishers and app launch (including
background relaunch).

Verification: simulator build succeeded.

Diff

FindFamilyiOS/App/AppDelegate.swift
diff --git a/FindFamilyiOS/App/AppDelegate.swift b/FindFamilyiOS/App/AppDelegate.swiftindex 0f0c474f0d..af39166294 100644--- a/FindFamilyiOS/App/AppDelegate.swift+++ b/FindFamilyiOS/App/AppDelegate.swift@@ -19,8 +19,13 @@ final class AppDelegate: NSObject, UIApplicationDelegate {         Task { @MainActor in             // Kick off the network / identity layer.             await Networking.shared.start()-            // Begin location updates — this is the iOS analog of LocationTrackingService on Android.-            LocationsHandler.shared.startLocationUpdates()+            // Subscribe to sharing-state changes first so a toggle that fires+            // before the DB finishes loading is still picked up, then start+            // location work only when we're actually sharing with someone —+            // this is the iOS analog of LocationTrackingService on Android.+            // The same gate runs on background relaunch from location events.+            LocationsHandler.shared.beginMonitoringSharing()+            LocationsHandler.shared.startLocationUpdatesIfSharing()         }         return true     }
FindFamilyiOS/Location/Location.swift
diff --git a/FindFamilyiOS/Location/Location.swift b/FindFamilyiOS/Location/Location.swiftindex f50689d600..31367cb34c 100644--- a/FindFamilyiOS/Location/Location.swift+++ b/FindFamilyiOS/Location/Location.swift@@ -1,4 +1,5 @@ import Foundation+import Combine import CoreLocation import UIKit import UserNotifications@@ -19,6 +20,7 @@ final class LocationsHandler: ObservableObject {     private var background: CLBackgroundActivitySession?     private var session: CLServiceSession?     private var task: Task<Void, Never>?+    private var sharingWatchers = Set<AnyCancellable>()      private init() {         manager.allowsBackgroundLocationUpdates = true@@ -37,6 +39,7 @@ final class LocationsHandler: ObservableObject {      func startLocationUpdates() {         authorize()+        manager.allowsBackgroundLocationUpdates = true         guard task == nil else { return }         logger.info("Starting location updates")         task = Task { [weak self] in@@ -66,8 +69,77 @@ final class LocationsHandler: ObservableObject {     func stopLocationUpdates() {         task?.cancel()         task = nil+        background?.invalidate()         background = nil+        session?.invalidate()         session = nil+        manager.stopUpdatingLocation()+        manager.stopMonitoringSignificantLocationChanges()+        for region in manager.monitoredRegions {+            manager.stopMonitoring(for: region)+        }+        manager.stopMonitoringVisits()+        manager.allowsBackgroundLocationUpdates = false+        BackgroundTask.shared.stopHeartbeat()+        logger.info("Stopped location updates (not sharing with anyone)")+    }++    /// Whether we currently publish our location to anyone: any peer with+    /// `sendingEnabled`, any pending share auto-toggle timer (which may flip+    /// sharing back on by itself), or any live temporary link. Expired links+    /// are pruned here so they can't keep location alive forever.+    func isSharingWithAnyone() -> Bool {+        pruneExpiredTemporaryLinks()+        let now = Date()+        let users = Database.shared.usersSubject.value+        let me = Networking.shared.userid+        if users.contains(where: { $0.id != me && ($0.sendingEnabled || $0.sharingAutoToggleAt != nil) }) {+            return true+        }+        return Database.shared.temporaryLinksSubject.value.contains(where: { $0.deleteAt > now })+    }++    /// Start location work only when we're actually sharing with someone.+    /// Safe to call on every launch (including background relaunch): re-entrant,+    /// no-ops when updates are already running.+    func startLocationUpdatesIfSharing() {+        if isSharingWithAnyone() {+            BackgroundTask.shared.startHeartbeat()+            startLocationUpdates()+        } else {+            stopLocationUpdates()+        }+    }++    /// Re-evaluate the sharing gate whenever the sharing state changes: the+    /// per-user toggle, auto-toggle timers, and temporary links. Every write+    /// path funnels through these subjects, so this covers manual toggles,+    /// timer flips, link add/delete, and user add/delete.+    func beginMonitoringSharing() {+        guard sharingWatchers.isEmpty else { return }+        // Hop to the main thread like MainViewModel does: Database sends from+        // its serial queue, and a synchronous MainActor hop from there risks+        // deadlock against its queue.sync writes.+        Database.shared.usersSubject+            .dropFirst()+            .receive(on: DispatchQueue.main)+            .sink { [weak self] _ in Task { @MainActor in self?.startLocationUpdatesIfSharing() } }+            .store(in: &sharingWatchers)+        Database.shared.temporaryLinksSubject+            .dropFirst()+            .receive(on: DispatchQueue.main)+            .sink { [weak self] _ in Task { @MainActor in self?.startLocationUpdatesIfSharing() } }+            .store(in: &sharingWatchers)+    }++    /// Delete temporary links past their expiry so an unopened/undeleted link+    /// can't pin background location on indefinitely. (The per-tick cleanup in+    /// `runTick` only runs while sharing is active.)+    private func pruneExpiredTemporaryLinks() {+        let now = Date()+        for link in Database.shared.temporaryLinksSubject.value where link.deleteAt <= now {+            Database.shared.deleteTemporaryLink(link)+        }     } } @@ -102,12 +174,11 @@ final class BackgroundTask {      private let geocoder = CLGeocoder() -    private init() {-        startHeartbeat()-    }+    private init() {} -    private func startHeartbeat() {-        heartbeatTask?.cancel()+    /// Idempotent: safe to call on every launch and every sharing-state change.+    func startHeartbeat() {+        guard heartbeatTask == nil else { return }         heartbeatTask = Task { [weak self] in             while !Task.isCancelled {                 if let loc = self?.lastKnownLocation {@@ -118,6 +189,12 @@ final class BackgroundTask {         }     } +    func stopHeartbeat() {+        heartbeatTask?.cancel()+        heartbeatTask = nil+        lastKnownLocation = nil+    }+     func onLocationUpdate(_ loc: CLLocation, sleep: Bool) {         lastKnownLocation = loc         let now = Date()