-->

startMonitoringForRegion not always adding regions

2019-05-28 12:00发布

问题:

I can not get startMonitoringForRegion to consistently add regions.

        int i =0;
        for(Deals *d in  deals){
            NSLog(@"deal addRegionsInDealsArray %@",d.deal_id);
            if (d.latitude != NULL && d.longitude!=NULL && 
                ![d.latitude isEqualToNumber:[NSNumber numberWithInt:0 ]] && 
                ![d.longitude isEqualToNumber:[NSNumber numberWithInt:0 ]]) {

                CLLocationCoordinate2D dl = CLLocationCoordinate2DMake(
                [d.latitude doubleValue],
                [d.longitude doubleValue]);
                //NSLog(@"Lat addRegionsInDealsArray %@",d.latitude);

                CLRegion * clr = [[CLRegion alloc] initCircularRegionWithCenter:dl radius:radius identifier:d.deal_id];

                [locationManager startMonitoringForRegion:clr desiredAccuracy:kCLLocationAccuracyHundredMeters];
                NSLog(@"Region to add:%@",clr);
                [clr release];
                i++;
                if (i==10) {
                    break;
                }
            }
        }

I put the break after 10 because it never added more than 10 and would crash somewhere around 560 regions. even limiting it to 10 regins it sometimes does not add 10 but only 8 (or 7, 9 or 0). It is not the first 8 that get added.

Also if I have

NSLog(@"region count %i",[[locationManager monitoredRegions] count]);

just after the loo it always produces an error

ERROR,Time,309834541.397,Function,"Boolean CLClientCopyMonitoredRegions(__CLClient*, const __CFDictionary**)",Could not send successfully

Any suggestions on how to use startMonitoringForRegion successfully?

edit: added @try catch around the startMonitoringForRegion: and the [[locationManager monitoredRegions] count] which produces an error. they error is still produces but not caught. Also moved it all to NSTimer loop that fires every second in can it was repeated calls causing problem. still fails randomly. sometimes my first call fails.

Cheers

回答1:

In the Location Awareness Programming Guide

You should always be judicious when specifying the set of regions to monitor. Regions are a shared system resource and the total number of regions available systemwide is limited. For this reason, Core Location limits the number of regions that may be simultaneously monitored by a single application.



回答2:

are you using iPhone4 feature only works on that device, check with

 [CLLocationManager regionMonitoringAvailable] 


回答3:

This appears to be a database error or something analogous. For some period of time after the region is registered, the table is locked (or, again, whatever the equivalent is behind the scenes, though I'm guessing it's exactly that). Therefore you can't do a count. Try counting after a longer period.