CLLocationManager in background thread

萝らか妹 提交于 2019-12-08 07:39:24

问题


I am doing one application.In that i am using the CLLocationManager Class for getting the updated location latitude and longitude details.But i need to use this CLLocationManager in sepaate thread .I written my code like below.

- (void)viewDidLoad
{
 [NSThread detachNewThreadSelector:@selector(fetch) toTarget:self withObject:nil];
}
-(void)fetch
 {
    manager=[[CLLocationManager alloc]init];
    manager.delegate=self;
    manager.distanceFilter=kCLDistanceFilterNone;
    manager.desiredAccuracy = kCLLocationAccuracyBest;
    [manager startUpdatingLocation];

 }
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
 {
   NSLog(@"%f",newLocation.coordinate.latitude);
   lbl.text=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
 }

.But this delegate method is not fired when i run this code.So please guide me how to get the location updates in separate thread.


回答1:


The methods of your delegate object are called from the thread in which you started the corresponding location services. That thread must itself have an active run loop, like the one found in your application’s main thread. ——from apple document




回答2:


Could you please tried with this.

 dispatch_async(newThread, ^(void) {

       [self fetch];
   }); 

hope you'll get solved problem.




回答3:


in .h file

MainView *ctl;
NSMutableDictionary *dictSubPoses;

- (id)initWithCtl:(MainView*)_ctl;

in .m file

- (id)initWithCtl:(MainView*)_ctl
{
   if(self = [super init]) 
   {
      ctl = _ctl; //[_ctl retain];
   }

   return self;
}

- (void)main 
{
   [ctl performSelector:@selector(yourMethod) withObject:dictSubPoses];
}



回答4:


in .h file

#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

//Set Delegate  
    CLLocationManagerDelegate
// Declare
    CLLocationManager *locationManager;

in .m file

-(void)ViewDidLoad
{
    locationupadate=YES;
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = 100.0f;
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
{

     if(locationupadate)
     {
           NSLog(@"%f",newLocation.coordinate.latitude);
          lbl.text=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];

     }


来源:https://stackoverflow.com/questions/13909527/cllocationmanager-in-background-thread

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!