iosobjective-ccllocationmanageribeaconclbeaconregion

Does stopMonitoringForRegion stops only the duplicated region or also the original region as well?


I am working on a beacon monitoring project and currently I am investigating the stopMonitoringForRegion method. What I am doing is, duplicate the original region and start a new monitoring for it. After 30 - 60 seconds, I stop the copied region:

[self.locationManager stopMonitoringForRegion:self.duplicatedRegion];

So the original and the duplicated regions minor and major ids are same. When I stop monitoring the duplicated region am I also stoping the original region? Because the minor and major ids are same. The only difference in between the original and duplicated region is the identifiers. The original one has a Vendor GUID whereas the duplicated has "testRegion" string.

self.duplicatedRegion = [[CLBeaconRegion alloc] initWithProximityUUID:self.originalRegion.proximityUUID
                                                                  major:[self.originalRegion.major intValue]
                                                                  minor:[self.originalRegion.minor intValue]
                                                             identifier:@"testRegion"];
        self.duplicatedRegion.notifyEntryStateOnDisplay = NO;
        self.duplicatedRegion.notifyOnEntry = YES;
        self.duplicatedRegion.notifyOnExit = YES;

        [self.locationManager startMonitoringForRegion:self.duplicatedRegion];

I am asking this because when I stop monitoring the duplicated region, the app doesn't receive any enter and exit region events anymore. In my code I am checking whether the original region is being monitored and yes it is on the self.locationManager monitoredRegions array. Also I never stop the original region. I am just start and stoping the duplicated one.


Solution

  • The critical point is to use a different string for the identifier field:

    self.duplicatedRegion = [[CLBeaconRegion alloc] initWithProximityUUID:self.originalRegion.proximityUUID                                                      
    major :[self.originalRegion.major intValue]                                                 
    minor :[self.originalRegion.minor intValue]                                                        
    identifier :@"testRegion"];
    

    If originalRegion has an identifier of testRegion and the duplicated one has that same value, it will cause the symptoms you describe. This is because CoreLocation uses the identifier field to signal that the Region objects are the same, allowing you to replace one object with another.

    To have both regions active at the same time, simply supply a different identifier value.