我正在尝试使用 iPhone SDK 为位置更新实现(非并发)NSOperation。NSOperation 子类的“肉”是这样的:
- (void)start {
// background thread set up by the NSOperationQueue
assert(![NSThread isMainThread]);
if ([self isCancelled]) {
return;
}
self->locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = self->desiredAccuracy;
locationManager.distanceFilter = self->filter;
[locationManager startUpdatingLocation];
[self willChangeValueForKey:@"isExecuting"];
self->acquiringLocation = YES;
[self didChangeValueForKey:@"isExecuting"];
}
- (void)cancel {
if ( ! self->cancelled ) {
[self willChangeValueForKey:@"isCancelled"];
self->cancelled = YES;
[self didChangeValueForKey:@"isCancelled"];
[self stopUpdatingLocation];
}
}
- (BOOL)isExecuting {
return self->acquiringLocation == YES;
}
- (BOOL)isConcurrent {
return NO;
}
- (BOOL)isFinished {
return self->acquiringLocation == NO;
}
- (BOOL)isCancelled {
return self->cancelled;
}
- (void)stopUpdatingLocation {
if (self->acquiringLocation) {
[locationManager stopUpdatingLocation];
[self willChangeValueForKey:@"isExecuting"];
[self willChangeValueForKey:@"isFinished"];
self->acquiringLocation = NO;
[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];
}
locationManager.delegate = nil;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
assert(![NSThread isMainThread]);
// ... I omitted the rest of the code from this post
[self stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)theError {
assert(![NSThread isMainThread]);
// ... I omitted the rest of the code from this post
}
现在,在主线程上,我创建了这个操作的一个实例并将它添加到一个 NSOperationQueue。start 方法被调用,但是没有一个-locationManager:...
委托方法被调用。我不明白为什么他们从来没有接到电话。
我确实让接口遵守了<CLLocationManagerDelegate>
协议。我让 NSOperationQueue 管理这个操作的线程,所以它应该都符合 CLLocationManagerDelegate 文档:
您的委托对象的方法是从您启动相应位置服务的线程中调用的。该线程本身必须有一个活动的运行循环,就像在应用程序的主线程中找到的那样。
我不确定还有什么可以尝试的。也许它正盯着我的脸......感谢任何帮助。
提前致谢!