2012-07-06 60 views
1
for (NSString * district in allLinedStrings) { 
    PO1(district); 
    [self.mainLock lock]; 
    CLGeocoder * geocode= [[CLGeocoder alloc]init]; 
    [geocode geocodeAddressString:district completionHandler:^(NSArray *placemarks, NSError *error) 
    { 
     for (CLPlacemark * thePlace in placemarks) 
     { 
      [self handlePlacemark:thePlace]; 

     } 
     [self.mainLock unlock]; 
    }]; 
} 

我想同步運行geocodeAddressString,我這樣做。不知何故,我有錯誤的僵局。但是,怎麼了?這個鎖定方案有什麼問題

+0

*我想同步運行geocodeAddressString,我這樣做* - 的API是異步的一個原因。你應該遵循這個原則,並重新啓動你的程序。例如等待'allLinedStrings'數組,處理下一個完成時調用... – justin 2012-07-06 09:35:52

回答

1

如果您使用的是NSLock:在同一個線程上調用兩次鎖定方法會永久鎖定您的線程。

for (NSString * district in allLinedStrings) { 
    PO1(district); 
    [self.mainLock lock]; 
    CLGeocoder * geocode= [[CLGeocoder alloc]init]; 
    [geocode geocodeAddressString:district completionHandler:^(NSArray *placemarks, NSError *error) 
    { 
     for (CLPlacemark * thePlace in placemarks) 
     { 
      [self handlePlacemark:thePlace]; 

     } 

    }]; 
[self.mainLock unlock]; 
} 
+0

好吧,我們不知道'self.mainLock'是否是遞歸鎖... – justin 2012-07-06 09:30:33

+0

小心解決這個問題?我認爲它應該等到解鎖。 – 2012-07-12 05:46:43