PDA

View Full Version : Threading with LocationManager




oceandrive
Dec 11, 2008, 03:15 PM
Case:

I need to get the Lat Long and submit to a web server.
I followed the sample codes to get the lat/long using the LocationManager and then was able to submit the data to my webserver using the NSURL.

I want to start a new thread on each click of a button on my app (which would do the above process) because I do not want to wait for the LocationManager to complete the task of getting Lat/Long data (Which is taking few seconds time and the app does not allow me to click on any other buttons).

So I started of like this.

[ NSThread detachNewThreadSelector: @selector(myThreadedMethod) toTarget: self withObject: nil ];


- (void)myThreadedMethod
{
NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
[locmanager startUpdatingLocation];
[ pool release ];
}

But when I do this my didUpdateToLocation: is not getting called.

Can someone guide me on if I am moving in the right direction or missing something.

Thanks for the help.



oceandrive
Dec 17, 2008, 08:28 AM
Anybody any suggestions for me????

Update:

I have now moved the submitToServer process to a Thread Method and it seems to be working fine, but still cant get the locationManager stuff to move to a Threaded method.

OnButtonPress

1) Get the Lat/Long
2) Create a new thread which does the submit to server.

But would still like to know how we can do the threading on the locationManager.

Thanks

rahulvyas
Dec 19, 2009, 06:24 AM
do not run/allocate location manager in another thread.Did you set CLLocationManagerDelegate in your header file? simulator will call the method didupdatetolocation only once while device call this multiple times.only allocate manager once and for getting new lat/long on button's click use [yourlocationmanagerObject startUpdatingLocation];