CLLocationManager stopUpdatingLocation 不会停止
CLLocationManager stopUpdatingLocation doesn't stop
下面是我的一些代码片段
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 应该处理这个问题。非常感谢任何见解。
谢谢,
didUpdateLocations:
由于其异步性质和 optimisations :
可以随时频繁调用
Because it can take several seconds to return an initial location, the
location manager typically delivers the previously cached location
data immediately and then delivers more up-to-date location data as it
becomes available. Therefore it is always a good idea to check the
timestamp of any location object before taking any actions. If both
location services are enabled simultaneously, they deliver events
using the same set of delegate methods.
实际上,如果您取消注释 locationManager = nil;
它应该会有帮助,但如果您想使用您的 checkingForLocation
属性,您应该使分配同步。 @synchronized(self)
是这种情况下最简单的方法。
下面是我的一些代码片段
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 应该处理这个问题。非常感谢任何见解。
谢谢,
didUpdateLocations:
由于其异步性质和 optimisations :
Because it can take several seconds to return an initial location, the location manager typically delivers the previously cached location data immediately and then delivers more up-to-date location data as it becomes available. Therefore it is always a good idea to check the timestamp of any location object before taking any actions. If both location services are enabled simultaneously, they deliver events using the same set of delegate methods.
实际上,如果您取消注释 locationManager = nil;
它应该会有帮助,但如果您想使用您的 checkingForLocation
属性,您应该使分配同步。 @synchronized(self)
是这种情况下最简单的方法。