1

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.

Shekhar Gupta
  • 6,206
  • 3
  • 30
  • 50
Kevin Char
  • 49
  • 1
  • 8

4 Answers4

2

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

AKA_Snail
  • 21
  • 2
-1

Could you please tried with this.

 dispatch_async(newThread, ^(void) {

       [self fetch];
   }); 

hope you'll get solved problem.

Sunil Targe
  • 7,251
  • 5
  • 49
  • 80
-2

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];

     }
Anusha Kottiyal
  • 3,855
  • 3
  • 28
  • 45
-2

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];
}
Anusha Kottiyal
  • 3,855
  • 3
  • 28
  • 45