diddeterminestate not always called

只谈情不闲聊 提交于 2019-12-09 17:53:47

问题


Does anyone else have a problem of having diddeterminestate not always being called? There are sometimes when I call

[self.locationManager requestStateForRegion:region]; 

and nothing happens. The curious thing is that when I insert a break point in

-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region

it starts working and being called! It seems very unstable to me.


回答1:


I often experienced this problem when requesting the state of a region right after monitoring it.

e.g.

[self.locationManager startMonitoringForRegion:region];
[self.locationManager requestStateForRegion:region];

I ensured that didDetermineStateForRegion was called by scheduling requestStateForRegion: shortly after calling startMonitoringForRegion. Now it's not a great solution and it should be used carefully but it seemed to fix this annoying issue for me. Code below

[self.locationManager startMonitoringForRegion:region];
[self.locationManager performSelector:@selector(requestStateForRegion:) withObject:region afterDelay:1];



回答2:


Perhaps putting the requestStateForRegion call inside the delegate method didStartMonitoringForRegion would be better. It's likely that the requestStateForRegion is being run before the monitoring (which is async) has actually started.

- (void) locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
    NSLog(@"Started Monitoring for Region:\n%@",region.description);
    [self.locationManager requestStateForRegion:region];
}


来源:https://stackoverflow.com/questions/24543814/diddeterminestate-not-always-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!