MKOverlay需要很长时间才能显示异步调用

时间:2012-09-09 02:10:17

标签: ios mkmapview redraw mkoverlay dispatch-async

我正在使用MKMapView和MKPolyline来显示路线形状。当我的MKMapView上有一个注释并且那里已有另一个注释时,它会异步地向我的路线提供者发送路由调用并添加形状覆盖。

这是问题所在。如果我使用主线程作为请求,它将在请求正在进行时冻结MKAnnotation drop,但是当它返回时,它会很好地显示覆盖。相反,如果我将请求发送到异步队列,则注释掉落中没有冻结,并且当异步调用最终化时,它会添加叠加层,但MapView需要一段时间才能实现并实际绘制叠加层。有时需要20秒。这是代码。

//Action handler selector to drop the pin
-(void)dropWayPoint:(WayPoint*)wp prevWayPoint:(WayPoint*)prevWp {
    [self.mapView addWayPoint: wp];
    [self.routeService routeFrom:prevWp to:wp];
}

实际的路线计算在这里:

@implementation RouteService

static dispatch_queue_t queue;

+(RouteService) initRouteService:(id)delegate {
    RouteService *rs = [[RouteService alloc] init];
    //Lots of things happen here, including the queue creation
    ...
    if (!queue) {
        queue = dispatch_queue_create("RouteDispatch.queue", NULL);
    }
    return rs;
}
//Lots of methods...
//then
-(void)routeFrom:(WayPoint*)wp to:(WayPoint*)wpTp {
    dispatch_async(queue, ^{
        //Code here
        DirectionsRouteRequest *rt = [DirectionsRouteRequest buildWithRouteType:@"fastest"];
        [rt addObjectToLocation:[wp coord]];
        [rt addObjectToLocation:[wpTp coord]];
        rt.options.routeType = @"fastest";

        NSLog(@"Dispatching...");

        //Now send the request
        DirectionsResponseType *response = [MapUtil computeDirections:rt];

        Leg *leg = [Leg buildFromResponse:response route: self.route startWayPoint:wp endWayPoint:wpTp];
        MKPolyline *pl = [MapUtil makePolylineWithLocations:[leg routeShape]];
        [self.route.legsHash setObject:pl forKey:leg.legIdStr];

        //Add Overlay here!!!
        [self.mapDeskViewController.mapView addOverlay: pl];
        //Desperately advising map view to redraw itself and show the overlay
        [self.mapDeskViewController.mapView setNeedsDisplay];

        //I can see this being displayed very quickly, meaning 
        NSLog(@"Response, pl_count:%d",pl.pointCount);

        //However it takes a long time after this returns to actually display the overlay.
    });
}

如果我使用上面的代码并注释掉async指令,它需要相同的时间来处理它,引起一个恼人的冻结,但是立即绘制叠加层:

//Lots of methods...
//then
-(void)routeFrom:(WayPoint*)wp to:(WayPoint*)wpTp {
    /* COMMENTED OUT ASYNC
    dispatch_async(queue, ^{
    */
        //Code here
        DirectionsRouteRequest *rt = [DirectionsRouteRequest buildWithRouteType:@"fastest"];
        [rt addObjectToLocation:[wp coord]];
        [rt addObjectToLocation:[wpTp coord]];
        rt.options.routeType = @"fastest";

        NSLog(@"Dispatching...");

        //Now send the request
        DirectionsResponseType *response = [MapUtil computeDirections:rt];

        Leg *leg = [Leg buildFromResponse:response route: self.route startWayPoint:wp endWayPoint:wpTp];
        MKPolyline *pl = [MapUtil makePolylineWithLocations:[leg routeShape]];
        [self.route.legsHash setObject:pl forKey:leg.legIdStr];

        //Add Overlay here!!!
        [self.mapDeskViewController.mapView addOverlay: pl];
        //Desperately advising map view to redraw itself and show the overlay
        [self.mapDeskViewController.mapView setNeedsDisplay];

        //I can see this being displayed very quickly, meaning 
        NSLog(@"Response, pl_count:%d",pl.pointCount);
    /*
    COMMENTED OUT ASYNC
    });
    */
}

任何想法为什么MKMapView需要一段时间来实现它需要在异步完成时绘制叠加?

由于 奥雷利奥

1 个答案:

答案 0 :(得分:2)

您需要更新主线程上的地图视图(以及所有UI)。因此,在dispatch_async块中以及收到您的回复后:

   // Create another block that gets queued up in the main_queue, a default serial queue
   dispatch_async(dispatch_get_main_queue(), ^{

       Leg *leg = [Leg buildFromResponse:response route: self.route startWayPoint:wp endWayPoint:wpTp];
       MKPolyline *pl = [MapUtil makePolylineWithLocations:[leg routeShape]];
       [self.route.legsHash setObject:pl forKey:leg.legIdStr];

       //Add Overlay here!!!
       [self.mapDeskViewController.mapView addOverlay: pl];
       //Desperately advising map view to redraw itself and show the overlay
       [self.mapDeskViewController.mapView setNeedsDisplay];

       //I can see this being displayed very quickly, meaning 
       NSLog(@"Response, pl_count:%d",pl.pointCount);
    });