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 并且我这样做了。不知何故,我得到了死锁的错误。但是怎么了?

4

1 回答 1

1

如果您使用 NSLock:在同一线程上调用两次 lock 方法将永久锁定您的线程。

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];
}
于 2012-07-06T09:27:31.427 回答