下面是我的一些代码片段CLLocationManager stopUpdatingLocation不会停止
BOOL checkingForLocation;
.
.
.
- (IBAction)LocationButtonTapped:(id)sender {
[locationManager startUpdatingLocation];
checkingForLocation = YES;
}
.
.
.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
[locationManager stopUpdatingLocation];
// locationManager = nil;
if (checkingForLocation) {
checkingForLocation = NO;
NSLog(@"here");
// fetch data from server using location and present view controllers
}
}
didUpdateLocations被多次调用,因为是的LocationManager持续更新每startUpdatingLocation位置。我的应用程序有一个错误,if(checkingForLocation)
中的代码多次运行并多次呈现视图导致不期望的结果。
1)为什么stopUpdatingLocation不会停止对didUpdateLocations的进一步调用?
2)此外,如果checkingForLocation
已经被设置为NO
为什么到didUpdateLocations随后调用还是把checkingForLocation
为YES
。那里有种族条件吗?
通过搜索其他SO问题,我发现设置locationManager = nil
将停止对didUpdateLocations的额外调用,它确实解决了我的情况中的问题,但没有真正的解释。看来,stopUpdatingLocation应该照顾到这一点。任何见解都非常感谢。
谢谢
你在哪里调用'[locationManager startUpdatingLocation]; checkingForLocation = YES;'? – OgreSwamp
@OgreSwamp我在按下按钮时调用它。为了清楚起见,我添加了上面的IBAction方法。 – galenom