详解iPhone中在多线程下使用CLLocationManager


  本文标签:iPhone 多线程 线程 CLLocationManager

  详解iPhone中在多线程下使用CLLocationManager是本文要介绍的内容,主要是介绍在多线程下如何使用CLLocationManager,一起来看本文详细内容讲解  。

  如果是子线程中创建CLLocationManager,那么startUpdatingLocation后是无法定位的,任何代理函数都不会被调用,而且表面上还会有提示是否定位的MessageBox,一切看起来都正常,就是代理不会执行  。

  似乎定位的返回(调用代理)只能有主线程来调用,并且这个对象还必须是在主线程创建的  。

  做过以下实验:

  1.子线程中:

  1. self.locationManager = [[CLLocationManager alloc] init] autorelease];  
  2. locationManager.delegate = self;  
  3. [locationManager startUpdatingLocation]; 

  结果:不会有任何结果返回  。

  2.主线程中:

  1. childThread.locationManager = [CLLocationManager alloc] init];  
  2. [childThread.locationManager release];  
  3. childThreadchildThread.locationManager.delegate = childThread; 

  在子线程中调用:

  1. [locationManager startUpdatingLocation]; 

  结果:代理函数会执行,但是是由主线程来调用的  。也就是子线程启动定位,主线程返回结果  。

  小结:详解iPhone中在多线程下使用CLLocationManager的内容介绍完了,希望本文对你有所帮助!