Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I can't see why it would appear to register the same region repeatedly. My suspicion would lead me to put some additional NSLog calls in there to verify the contents of your source, self.geofences. Another thing I would do is put a break in there to observe values and conditionals in your For loop.</p> <p>Two other items I would check:</p> <ol> <li>confirm that you are getting the expected result from <code>if(![self.locationManager.monitoredRegions containsObject:geofence]){</code> . The objects in CLLocationManager are not guaranteed to be identical to what you initially registered. You should be testing for the identifier proper using isEqualToString. It is possible you are replacing your regions unintentionally as you have it written.</li> <li>If the region was registered already, I don't think you need to call <code>didEnterRegion</code> . Boundary events are reported between launches. Even if the app was terminated it should handle this automatically at relaunch.</li> </ol> <p>If none of that sheds light on the issue, I would look at how and when you are starting/stopping location services. Is it possible that each of those calls are another iteration of the didUpdateLocations method?</p> <p>I expect that the first time that this is called, in a brand new session, monitoredRegions is empty so your process works as expected. I see reason for possible unpredictable behavior on subsequent location updates.</p> <p>I would start by changing your for loop completely with this.</p> <pre><code>if (locations &amp;&amp; locations.count) { CLLocation* position = [locations lastObject]; NSSet *regionIdentifiers = [self.locationManager.monitoredRegions valueForKey:@"identifier"]; NSSet *regionsForMonitoring = [NSSet setWithArray:self.geofences.allValues]; NSSet *unMonitoredRegions = [regionsForMonitoring objectsPassingTest:^BOOL(id obj, BOOL *stop) { CLCircularRegion *regionForTest = (CLCircularRegion *)obj; if (![regionIdentifiers containsObject:regionForTest.identifier]) { return YES; } else { return NO; } }]; for (CLCircularRegion *geofence in unMonitoredRegions) { [self.locationManager startMonitoringForRegion:geofence]; // We'll check if we are in the new region as it was not previously monitored if([geofence containsCoordinate:[position coordinate]]){ [self locationManager:manager didEnterRegion:geofence]; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload