Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why the didEnterRegion called twice?

I am using location services i.e. region monitoring in my iOS app, This is my code

//this for creating region
-(void)createRegion
{
    [dictionary setValue:@"23 St, New York" forKey:@"title"];
    [dictionary setValue:[NSNumber numberWithDouble:40.742878] forKey:@"latitude"];
    [dictionary setValue:[NSNumber numberWithDouble:-73.992821] forKey:@"longitude"];
    [dictionary setValue:[NSNumber numberWithDouble:(300.0)] forKey:@"radius"];

    [regionArray addObject:[self mapDictionaryToRegion:dictionary]];
    [self initializeRegionMonitoring:regionArray];
}
- (CLRegion*)mapDictionaryToRegion:(NSDictionary*)dictionary {
    NSString *title = [dictionary valueForKey:@"title"];

    CLLocationDegrees latitude = [[dictionary valueForKey:@"latitude"] doubleValue];
    CLLocationDegrees longitude =[[dictionary valueForKey:@"longitude"] doubleValue];
    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);

    CLLocationDistance regionRadius = [[dictionary valueForKey:@"radius"] doubleValue];

    return [[CLRegion alloc] initCircularRegionWithCenter:centerCoordinate
                                                   radius:regionRadius
                                               identifier:title];
}

- (void) initializeRegionMonitoring:(NSArray*)geofences {
    if(![CLLocationManager regionMonitoringAvailable]) {
       // [self showAlertWithMessage:@"This app requires region monitoring features which are unavailable on this device."];
        return;
    }

    for(CLRegion *geofence in geofences) {
        [_locationManager startMonitoringForRegion:geofence];
    }
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"entered region %@",region.identifier);
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"exited region %@",region.identifier);
}

It works fine when app is in foreground. It shows me log :"entered region.." and "exited region ..", but when app goes to background then same logs are printed twice in just fraction of second, i.e. delegate methods called twice, that i don't need, is there any way to avoid calling 2 times? or is am doing any mistake while creating or monitoring regions ? please help me .. thanks in advance ..

like image 404
prabhu Avatar asked Dec 27 '12 11:12

prabhu


1 Answers

I believe this question is a duplicate of my own. I think there is a bug with Apple's region monitoring APIs. I have filed a radar for this issue with Apple already, but have not heard anything back as of yet.

One way I get around this is by saving the timestamp in the didEnter and didExit methods and if the methods fire within 10 seconds of the saved timestamp, just skip the method as a dupe.

If anyone is interested, I have a project on github showing this problem in action.

https://github.com/billburgess/GeofenceBug

Feel free to file another radar as that is the only way Apple will notice the issue and take action. The radar number is 12452255 - Duplicate Delegate calls for region monitoring.

Here is the open radar link with the information if you would like to dupe this radar. http://openradar.appspot.com/radar?id=2484401

like image 180
Bill Burgess Avatar answered Oct 23 '22 16:10

Bill Burgess