Objective c iOS:使用GCD优化UIViewController和/或MKMapKit MKPolyline?

Objective c iOS:使用GCD优化UIViewController和/或MKMapKit MKPolyline?,objective-c,multithreading,concurrency,grand-central-dispatch,mkpolyline,Objective C,Multithreading,Concurrency,Grand Central Dispatch,Mkpolyline,我正在制作一个使用坐标数据的应用程序。我的应用程序将启动,另一个应用程序在进入此阶段之前必须通过不同的视图控制器。用户将在输入此特定的UIViewController之前选择坐标路线 目前,为了测试目的,我在viewDidLoad中完成了所有这些工作。此视图控制器的目的是基于预先确定的路线显示带有MKPolyline的地图。MKPolyline将是一个基于系数的渐变,在这种情况下,velocity GradientPolylineOverlay来自和 我的主要问题是,在代码保持不变的情况下,MK

我正在制作一个使用坐标数据的应用程序。我的应用程序将启动,另一个应用程序在进入此阶段之前必须通过不同的视图控制器。用户将在输入此特定的
UIViewController
之前选择坐标路线

目前,为了测试目的,我在
viewDidLoad
中完成了所有这些工作。此视图控制器的目的是基于预先确定的路线显示带有
MKPolyline
的地图。
MKPolyline
将是一个基于系数的渐变,在这种情况下,
velocity

GradientPolylineOverlay
来自和

我的主要问题是,在代码保持不变的情况下,
MKPolyline
的渲染速度非常慢,并且
mapView
上的任何事件都非常缓慢和滞后

我已经确定,因为我有很多坐标(很容易超过7000),以下两行中的一行导致了问题:

[地图视图添加覆盖:多段线];
[self.view addSubview:mapView];

-(void)viewDidLoad
{
[超级视图下载];
data=delegate.data;//包含来自我的源的排序/分析数据
NSInteger pointsCount=[data sampleCount];
//设置地图视图
mapView=[[MKMapView alloc]initWithFrame:self.view.bounds];
mapView.delegate=self;
mkcoordinaereregion=mkcoordinaereregionmakewithdistance(*[data getgpscoordinates for sample:0],500500);
[地图视图设置区域:区域];
CLLocationCoordination2D点使用[pointsCount];
浮点数*速度=0;
速度=malloc(sizeof(float)*pointscont);
//pointCount包含7000个坐标!
for(int i=0;i纬度,[data GetGPScoordinates for Sample:i]->经度);
使用点[i]=CLLocationCoordination2dMake(p.x,p.y);
速度[i]=[data getGPSVelocityForSample:i];
}
polyline=[[GradientPolylineOverlay alloc]initWithPoints:pointsToUse velocity:velocity count:pointsCount];
//问题!
[地图视图添加覆盖:多段线];
[self.view addSubview:mapView];
自由(速度);
}
我相信这一点,为了提高性能,我应该使用Grand Central Dispatch。使用GCD,我将代码更改为:

dispatch_queue_t queue = dispatch_queue_create("Test", 0);
dispatch_async(queue, ^{

    CLLocationCoordinate2D pointsToUse[pointsCount];


    float* velocity =0;
    velocity = malloc(sizeof(float)*pointsCount);

    // pointCount contains 7000 coordinates!
    for(int i = 0; i < pointsCount; i++) {
        CGPoint p = CGPointMake([data getGPSCoordinatesForSample:i]->latitude,[data getGPSCoordinatesForSample:i]->longitude);
        pointsToUse[i] = CLLocationCoordinate2DMake(p.x,p.y);
        velocity[i] =[data getGPSVelocityForSample:i];
    }


    polyline = [[GradientPolylineOverlay alloc] initWithPoints:pointsToUse velocity:velocity count:j];
    [mapView addOverlay:polyline]; // Crash
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.view addSubview:mapView];
    });
});
dispatch\u queue\u t queue=dispatch\u queue\u create(“测试”,0);
调度异步(队列^{
CLLocationCoordination2D点使用[pointsCount];
浮点数*速度=0;
速度=malloc(sizeof(float)*pointscont);
//pointCount包含7000个坐标!
for(int i=0;i纬度,[data GetGPScoordinates for Sample:i]->经度);
使用点[i]=CLLocationCoordination2dMake(p.x,p.y);
速度[i]=[data getGPSVelocityForSample:i];
}
polyline=[[GradientPolylineOverlay alloc]initWithPoints:Points使用速度:速度计数:j];
[mapView addOverlay:多段线];//崩溃
dispatch\u async(dispatch\u get\u main\u queue()^{
[self.view addSubview:mapView];
});
});
[mapView添加覆盖:多段线]发出
线程4:EXC\u BAD\u访问
调试错误

我的理想解决方案必须使用所有坐标,但如果需要,我可以缩小比例。如果我可以在代码中的某个地方使用GCD,那么最好的地方在哪里


谢谢大家!

我认为
mapView:rendererForOverlay
mapViewDidFinishLoadingMap
是一个不错的选择

它们都在MKMapViewDelegate中

dispatch_queue_t queue = dispatch_queue_create("Test", 0);
dispatch_async(queue, ^{

    CLLocationCoordinate2D pointsToUse[pointsCount];


    float* velocity =0;
    velocity = malloc(sizeof(float)*pointsCount);

    // pointCount contains 7000 coordinates!
    for(int i = 0; i < pointsCount; i++) {
        CGPoint p = CGPointMake([data getGPSCoordinatesForSample:i]->latitude,[data getGPSCoordinatesForSample:i]->longitude);
        pointsToUse[i] = CLLocationCoordinate2DMake(p.x,p.y);
        velocity[i] =[data getGPSVelocityForSample:i];
    }


    polyline = [[GradientPolylineOverlay alloc] initWithPoints:pointsToUse velocity:velocity count:j];
    [mapView addOverlay:polyline]; // Crash
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.view addSubview:mapView];
    });
});