ios - CLLocationManager didUpdateToLocation :, 在哪个线程上?

标签 ios objective-c multithreading nstimer cllocationmanager

我目前正在尝试在 CLLocationManager 委托(delegate)调用上实现超时功能。 代码如下:

- (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 在哪个线程上调用其委托(delegate)的方法?
  3. 如果它不是主线程,我可能会遇到评论突出显示它的问题。我怎样才能避免这种情况?

最佳答案

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

第一个线程是主线程

enter image description here

关于ios - CLLocationManager didUpdateToLocation :, 在哪个线程上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17022607/

相关文章:

c# - 如何使用 StreamReader(或其他替代方法)在 IOS 应用程序中的 Xamerin 上将文件读入数组

ios - 如何在不使用 Storyboard的情况下创建 UICollectionView?

iphone - 子类化 UIToolbar 的正确方法

java - 无需重新启动即可生成 Java 线程转储。

java - ExecutorService 终止,但 JVM 没有

java - 主线程上的 View.postDelayed() 和 Handler.postDelayed() 有什么区别?

ios - 停止画外音说出 UIAccessibilityTrait 可调的自动提示(快速)

iphone - cocos2d :how to create continous parallax layer

ios - 在 iOS 中推送 View Controller 时难以绕过堆栈中的 View Controller

iphone - 新的 UIView 未加载