Post

Replies

Boosts

Views

Activity

Reply to CLMonitor Add region after starting to monitor for event changes
Forgot to give an explanation for how I think this fix works: My understanding is that CLMonitor.events will only contain events from regions that were added to the CLMonitor before running the for loop. Adding a new region to the CLMonitor inside of the for loop means that the new region's event updates will not appear in the CLMonitor.events collection in this loop. The workaround then is to stop listening for events and restart the for loop.
Nov ’23
Reply to CLMonitor Add region after starting to monitor for event changes
I managed to create a workaround that is poor code design but should solve the issue. func startMonitoringConditions() { Task { monitor = await CLMonitor(MonitorNames.monitorName) if let identifiers = await monitor?.identifiers { if identifiers.count == 0 { if let coordinate = manager.location?.coordinate { await addNewRegionAtCoordinate(coordinate: coordinate) } } else { print("Previous Monitor Region is used.") } } while continueTrackingRegions { for try await event in await monitor!.events { if let coordinate = manager.location?.coordinate { // do something await monitor!.remove(event.identifier) await addNewRegionAtCoordinate(coordinate: coordinate) break } } } } } This update seems to work for me although I am unable to fully confirm if it works 100% of the time because I am now running into an issue with the new SwiftData API. Gotta love ModelContexts...
Nov ’23