How to reset region center when exited region in background using CoreLocation

104 views Asked by At

My idea is simple in theory, but am having a really hard time executing it. I want to create a geofence at the user's location, and when the user exits that region, I want to recenter the region at the user's updated location. Here is the flow:

  1. Retrieve user's currently location using standard [CLLocationManager startUpdatingLocation]
  2. On locationManager:didUpdateLocations:, create a geofence at the current location using startMonitoringForRegion, and on subsequent location updates, recenter that region
  3. On locationManager:didExitRegion:, which is usually called from the background to wake up the app, call [CLLocationManager startUpdatingLocation] to retrieve the user's current location to recenter the geofence (centered to the next updated location that is NOT the current region center coordinate).

Sometimes this all works, but more often than not, the geofence doesn't recenter correctly. What's been happening is that the region is re-centered and locationManager:didExitRegion: is immediately called, which puts it into an infinite loop until the app is terminated again. Since it is terminated without a valid monitored region, the app then won't be woken up for location updates.

Is this incorrect usage of region monitoring? I'm trying to play with different ways of tracking user location in a battery efficient way (and not constantly using the standard startUpdatingLocation). Would a combination of signification location change tracking and visit monitoring be more effective?

Thanks!

0

There are 0 answers