2013-01-15 31 views
1

設置地址字符串我嘗試本地化開始和結束點到地址字符串,以便我可以將它存儲到NSUserDefaults。問題是該方法繼續執行,並沒有設置我的變量。使用reverseGeocodeLocation設置地址字符串並從方法

NSLog(@"Begin"); 

__block NSString *returnAddress = @""; 

[self.geoCoder reverseGeocodeLocation:self.locManager.location completionHandler:^(NSArray *placemarks, NSError *error) { 
    if(error){ 
     NSLog(@"%@", [error localizedDescription]); 
    } 

    CLPlacemark *placemark = [placemarks lastObject]; 

    startAddressString = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@", 
          placemark.subThoroughfare, placemark.thoroughfare, 
          placemark.postalCode, placemark.locality, 
          placemark.administrativeArea, 
          placemark.country]; 
    returnAddress = startAddressString; 

    //[self.view setUserInteractionEnabled:YES]; 
}]; 
NSLog(returnAddress); 

NSLog(@"Einde"); 

這是我的應用程序調試器顯示:

開始
einde

舉例來說,如果我的位置的地址是: 「主街32 CITY」。那麼我想看到的是以下內容:

開始
主街32,CITY
Einde

的問題是,我的代碼不會等待我的CLGeocoder來完成,所以我變量returnAddress在返回時未設置,並且爲空。

有誰知道如何解決這個問題?

回答

5

因爲reverseGeocodeLocation有一個完成塊,當它執行到達時它會被切換到另一個線程 - 但主線程上的執行仍然會繼續到下一個操作,即NSLog(returnAddress)。此時,returnAddress尚未設置,因爲reverseGeocodeLocation只是已交給另一個線程。

使用完成塊時,您必須開始考慮異步工作。

考慮將reverseGeocodeLocation作爲方法中的最後一個操作,然後使用完成塊中的其餘邏輯調用新方法。這將確保在returnAddress的值爲0之前邏輯不會執行。

- (void)someMethodYouCall 
{ 
    NSLog(@"Begin"); 
    __block NSString *returnAddress = @""; 

    [self.geoCoder reverseGeocodeLocation:self.locManager.location completionHandler:^(NSArray *placemarks, NSError *error) { 
     if(error){ 
      NSLog(@"%@", [error localizedDescription]); 
     } 

     CLPlacemark *placemark = [placemarks lastObject]; 

     startAddressString = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@", 
           placemark.subThoroughfare, placemark.thoroughfare, 
           placemark.postalCode, placemark.locality, 
           placemark.administrativeArea, 
           placemark.country]; 
     returnAddress = startAddressString; 

     //[self.view setUserInteractionEnabled:YES]; 

     NSLog(returnAddress); 
     NSLog(@"Einde"); 

     // call a method to execute the rest of the logic 
     [self remainderOfMethodHereUsingReturnAddress:returnAddress]; 
    }]; 
// make sure you don't perform any operations after reverseGeocodeLocation. 
// this will ensure that nothing else will be executed in this thread, and that the 
// sequence of operations now follows through the completion block. 
} 

- (void)remainderOfMethodHereUsingReturnAddress:(NSString*)returnAddress { 
    // do things with returnAddress. 
} 

或者您可以使用NSNotificationCenter發送通知時reverseGeocodeLocation完成。您可以在需要的任何地方訂閱這些通知,並從那裏完成邏輯。替換爲[self remainderOfMethodHereWithReturnAddress:returnAddress];

NSDictionary *infoToBeSentInNotification = [NSDictionary dictionaryWithObject:returnAddress forKey:@"returnAddress"]; 

[[NSNotificationCenter defaultCenter] 
    postNotificationName:@"NameOfNotificationHere" 
    object:self 
    userInfo: infoToBeSentInNotification]; 
    }]; 

Here's使用NSNotificationCenter的一個例子。

+0

是的,我知道,但在我的情況下,我需要在完成塊外使用該變量。因爲我在塊之外還有其他一些邏輯,所以我不能放在塊內。你知道一個辦法嗎?你有沒有鏈接或樣本我的異步線程?謝謝 – Wesley

+0

我已經用一些方法更新了答案,您可以用您需要的返回地址執行剩餘的邏輯。 –

+0

太棒了,我已經想通了!謝謝。 – Wesley

相關問題