سؤال

أحاول تنفيذ nsoperation (غير متطابق) لتحديثات الموقع باستخدام iPhone SDK. "اللحوم" من الفئة الفرعية 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. يتم استدعاء طريقة البدء ، ولكن لا شيء من -locationManager:... يتم استدعاء أساليب المفوض. أنا لا أفهم لماذا لم يتم الاتصال بهم.

لقد جعلت الواجهة تلتصق بـ <CLLocationManagerDelegate> بروتوكول. أنا أترك nsoperationqueue إدارة مؤشر الترابط لهذه العملية ، لذلك يجب أن تكون جميعها مطابقة لوثائق cllocationManagerDelegate:

يتم استدعاء طرق كائن المندوب الخاص بك من مؤشر الترابط الذي بدأت فيه خدمات الموقع المقابلة. يجب أن يكون لهذا الموضوع بحد ذاته حلقة تشغيل نشطة ، مثل تلك الموجودة في الخيط الرئيسي للتطبيق الخاص بك.

لست متأكدًا مما يجب محاولة هذا العمل. ربما يحدق بي في وجهي ... أي مساعدة موضع تقدير.

شكرا لك مقدما!

هل كانت مفيدة؟

المحلول

أنت تفتقد جزء "حلقة التشغيل النشطة". في نهاية طريقة البدء الخاصة بك إضافة:

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top