【问题标题】:Return value in delegate method but with completion hander委托方法中的返回值,但带有完成处理程序
【发布时间】:2015-10-12 06:43:19
【问题描述】:

我有一个带有视图控制器的地图作为它的委托,我调用下面的方法向地图添加一些自定义引脚并获取到它们的路线。

我想添加路线到引脚的距离,所以我需要计算路线,然后在返回引脚之前添加到自定义引脚的距离。

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
     //setup custom pin view and directions request

    [directions calculateDirectionsWithCompletionHandler:
    ^(MKDirectionsResponse *response, NSError *error) {
         if (error) 
         {
              // error
         } else {
             // no error
         }
     }];

     // add distance to custom pin view
     // ONLY once directions calculated
     return custom pin view;
}

有什么想法可以解决这个问题吗?

【问题讨论】:

    标签: multithreading return return-value objective-c-blocks completionhandler


    【解决方案1】:

    我认为您需要在不等待的情况下返回视图,然后在完成处理程序中对其进行更新。可以使用NSCondition(可能还有其他方式)同步异步调用,但是当主线程用于完成时,这很危险。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      相关资源
      最近更新 更多