4

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 のドキュメントに準拠している必要があります。

デリゲート オブジェクトのメソッドは、対応するロケーション サービスを開始したスレッドから呼び出されます。そのスレッド自体に、アプリケーションのメイン スレッドにあるようなアクティブな実行ループが必要です。

これが機能するために他に何を試すべきかわかりません。多分それは私をじっと見つめています...どんな助けも大歓迎です。

前もって感謝します!

4

1 に答える 1

6

「アクティブな実行ループ」の部分がありません。start メソッドの最後に以下を追加します。

while (![self isCancelled])
  [[NSRunLoop currentRunLoop] runUntilDate:someDate];

于 2010-10-22T20:14:21.487 に答える