【问题标题】:CLLocationManager didUpdateToLocation:, on which thread?CLLocationManager didUpdateToLocation:,在哪个线程上?
【发布时间】:2013-06-10 11:15:21
【问题描述】:

我目前正在尝试在 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. 如果它不是主线程,我可能会遇到 cmets 突出显示它的问题。我该如何规避呢?

【问题讨论】:

    标签: ios objective-c multithreading nstimer cllocationmanager


    【解决方案1】:

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

    第一个线程是主线程

    【讨论】:

    • 事实上,委托调用是在主线程上进行的。感谢您的宝贵意见。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    相关资源
    最近更新 更多