iOS block 同步执行(2)

    • (CLPlacemark*)getStartplacemark
  1. {
  2. //先创建一个semaphore
  3. dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
  4. dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
  5. __block CLPlacemark* start;
    
  6. [self.geoCoder geocodeAddressString:_SourceText.text completionHandler:^(NSArray *placemarks, NSError *error) {      
    
  7.   CLPlacemark *startPlaceMark = [placemarks firstObject];
    
  8.     start = startPlaceMark;
    
  9.    //发出已完成的信号
    
  10.     dispatch_semaphore_signal(semaphore);
    
  11. }];
    
  12. //等待执行,不会占用资源
    
  13. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    
  14. return start;
    
  15. }

https://www.zybuluo.com/MicroCai/note/64272

你可能感兴趣的:(iOS block 同步执行(2))