バックグラウンドスレッドでCLLocationの更新を受信
-
25-09-2019 - |
質問
私は、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>
プロトコルへのインタフェースの付着を行いました。それは、すべてのCLLocationManagerDelegateのドキュメントに準拠する必要がありますので、私は、NSOperationQueueこの操作にスレッドを管理せています:
は、デリゲートオブジェクトのメソッドを使用すると、対応するロケーションサービスを開始したスレッドから呼ばれています。 1は、アプリケーションのメインスレッドで見られるように、そのスレッドは、それ自体が、アクティブな実行ループを持っている必要があります。
私は仕事に、このために他に何をしようとすることを確認していません。多分それは私の顔を見つめています...すべてのヘルプは大歓迎です。
事前に感謝します!
解決
あなたは「アクティブ実行ループ」の部分が欠落しています。
while (![self isCancelled])
[[NSRunLoop currentRunLoop] runUntilDate:someDate];
タグ:あなたのstartメソッドの追加の終わりに
所属していません StackOverflow