CLLocationManager didUpdateToLocation:,在哪个线程上?

时间:2013-06-10 11:15:21

标签: ios objective-c multithreading nstimer cllocationmanager

我目前正在尝试在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;
        }
    }
}

如果你阅读了这些代码,你可能已经偶然发现了我的问题。

  1. 有没有更好的方法(=已经存在CLLocationManager)来实现这样的超时?
  2. CLLocationManager调用其委托方法的哪个线程?
  3. 如果它不是主线程,我可能会遇到问题,注释会突出显示它。我怎么能绕过那个?

1 个答案:

答案 0 :(得分:3)

你可以在方法中简单地添加断点,在xcode的左侧你会看到哪个线程。

第一个主题是主线程

enter image description here