views:

170

answers:

1

I have a query regarding how delegates work. My understanding was that delegates take responsibility for doing certain tasks on behalf of another object.

locationManager = [[CLLocationManager alloc] init]; 
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];

Am I right in thinking that in the example code above that the instance of CLLocationManager is created on a new thread so that it can get on with trying to find the location information it needs. When it completes its task (or encounters an error) it calls-back using the appropriate methods located in self e.g.

locationManager:didUpdateToLocation:fromLocation:

Essentially locationManager sends messages to self (which conforms to the correct delegate protocol) when things happen

cheers gary

+5  A: 

That is mostly correct.

the instance of CLLocationManager is created on a new thread

No, the instance is created on the thread you call it from. You posted no threading-related code. Of course the Location Manager avoids blocking the thread while working. This may be using a background thread internally, but you don't know or care.

locationManager sends messages to self (which conforms to the correct delegate protocol) when things happen

Yes.

Mike Abdullah
Just to emphasize the point: Instead of a background thread, the location manager could also just have a polling timer, or register for a callback.
Chuck
thank you, I was just curious as particularly with CLLocationManager once you set it going it goes off and does its own thing. I guess however its really just waiting for the iPhone hardware to work out where the device is so its not really doing a whole load of processing itself.
fuzzygoat