Uso de delegados na NSOperation
Estou tentando fazer uso deCLLocationManager
em umNSOperation
. Como parte disso, eu preciso da capacidade destartUpdatingLocation
aguarde até que um CLLocation seja recebido antes de concluir a operação.
No momento, eu fiz o seguinte, no entanto, o método delegado nunca parece ser chamado. Alguém pode aconselhar qual é o problema?
- (void)main
{
@autoreleasepool {
if (self.isCancelled)
return;
// Record the fact we have not found the location yet
shouldKeepLooking = YES;
// Setup the location manager
NSLog(@"Setting up location manager.");
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
while (shouldKeepLooking) {
if (self.isCancelled)
return;
// Do some other logic...
}
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// None of this ever seems to be called (despite updating the location)
latestLocation = [locations lastObject];
[manager stopUpdatingLocation];
shouldKeepLooking = NO;
}