我目前正在尝试在CLLocationManager
委托调用中实现超时功能。这是代码:
- (void)checkInAt:(UALocation *)location timeout:(NSTimeInterval)timeout {
NSDate *tickDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
self.locationManager = [[CLLocationManager alloc] init];
self.timeout = [[NSTimer alloc] initWithFireDate:tickDate interval:0 target:self selector:@selector(timedOut:) userInfo:nil repeats:NO];
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
@synchronized (self.timeout) {
// Todo what if the timer fires right now?
// Is it even feasible? On what thread is CLLocationManager operating?
if (self.timeout.isValid) {
[self.timeout invalidate];
}
else {
// Timed out
return;
}
}
[manager stopUpdatingLocation];
// Do the actual check in
}
- (void)timedOut:(NSTimer *)timer {
@synchronized (timer) {
if (!timer.isValid) {
return;
}
}
}
如果您阅读了代码,您可能已经偶然发现了我的问题。
- 有没有更好的方法(= 已经存在
CLLocationManager
)来实现这样的超时? CLLocationManager
它的委托的调用方法在哪个线程上?- 如果它不是主线程,我可能会遇到评论突出显示它的问题。我该如何规避呢?