詳解iPhone中在多線程下使用CLLocationManager
詳解iPhone中在多線程下使用CLLocationManager是本文要介紹的內容,主要是介紹在多線程下如何使用CLLocationManager,一起來看本文詳細內容講解。
如果是子線程中創建CLLocationManager,那么startUpdatingLocation后是無法定位的,任何代理函數都不會被調用,而且表面上還會有提示是否定位的MessageBox,一切看起來都正常,就是代理不會執行。
似乎定位的返回(調用代理)只能有主線程來調用,并且這個對象還必須是在主線程創建的。
做過以下實驗:
1.子線程中:
- self.locationManager = [[CLLocationManager alloc] init] autorelease];
- locationManager.delegate = self;
- [locationManager startUpdatingLocation];
結果:不會有任何結果返回。
2.主線程中:
- childThread.locationManager = [CLLocationManager alloc] init];
- [childThread.locationManager release];
- childThreadchildThread.locationManager.delegate = childThread;
在子線程中調用:
- [locationManager startUpdatingLocation];
結果:代理函數會執行,但是是由主線程來調用的。也就是子線程啟動定位,主線程返回結果。
小結:詳解iPhone中在多線程下使用CLLocationManager的內容介紹完了,希望本文對你有所幫助!