这种锁定方案有什么问题

时间:2021-11-03 06:49:34
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];
     }];
}

I want to run geocodeAddressString synchronously and I do this. Somehow I got error of deadlock. But what's wrong?

我想同步运行geocodeAddressString,我这样做。不知怎的,我得到了死锁的错误。但是怎么了?

1 个解决方案

#1


1  

If you are using NSLock: Calling the lock method twice on the same thread will lock up your thread permanently.

如果您使用的是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];
}

#1


1  

If you are using NSLock: Calling the lock method twice on the same thread will lock up your thread permanently.

如果您使用的是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];
}