2013-06-10 3 views
2

J'essaye actuellement d'implémenter une fonction de délai d'attente sur un appel de délégué CLLocationManager. Voici le code:CLLocationManager didUpdateToLocation: sur quel thread?

- (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; 
     } 
    } 
} 

Si vous lisez le code, vous avez probablement trébuché sur mes questions déjà.

  1. Existe-t-il un meilleur moyen (= déjà existant dans CLLocationManager) de mettre en œuvre un tel délai?
  2. Sur quel thread le CLLocationManager appelle-t-il les méthodes de son délégué?
  3. Si ce n'est pas le thread principal, je pourrais potentiellement avoir un problème où les commentaires le mettent en évidence. Comment puis-je contourner cela?

Répondre

3

Vous pouvez simplement ajouter un point d'arrêt dans la méthode et sur le côté gauche dans le xcode, vous verrez quel thread est.

fil est d'abord thread principal

enter image description here

+0

En fait, l'appel de délégué est fait sur le thread principal. Merci pour votre contribution précieuse. – aspyct

Questions connexes