如何添加注释的MKMapView asyncronously? [英] How to add annotations to MKMapView asyncronously?

查看:220
本文介绍了如何添加注释的MKMapView asyncronously?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多的注释被添加到一个的MKMapView。当我添加注释,应用程序冻结的时间很短。我明白,主线程是不允许添加用户界面来查看,如果这是真的,怎么我做这个操作不会冻结应用程序的唯一主题?

  //在viewDidLoad中
对于(NSManagedObject *在requestResults对象){
     CustomAnnotation * customAnnotation = [[CustomAnnotation的alloc]初始化];
     customAnnotation.title = object.title;
     customAnnotation.coordinate = CLLocationCoordinate2DMake([object.latitude的doubleValue],[object.longitude的doubleValue]);
     [_mapView addAnnotation:customAnnotation];
}
} //结束viewDidLoad中 - (MKAnnotationView *)的MapView:(*的MKMapView)的MapView viewForAnnotation:(ID< MKAnnotation>)注释
{
//显示了具有自定义图像注释
    MKPinAnnotationView * customPinView = [[MKPinAnnotationView页头] initWithAnnotation:注释reuseIdentifier:@mapAnnotation];
    CustomAnnotation * customAnnotation =(ID)注释;
    customPinView.image = [UIImage的imageNamed:@绿色];
    返回customPinView;
}


解决方案

您可以使用<一个href=\"https://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html\"相对=nofollow>大中央调度 - GCD 这样做的。

试着用:

   - (无效)viewDidLoad中
{
  dispatch_async(dispatch_get_global_queue(0,0),^ {
     对于(NSManagedObject *在requestResults对象)
     {
        CustomAnnotation * customAnnotation = [[CustomAnnotation的alloc]初始化];
        customAnnotation.title = object.title;
        customAnnotation.coordinate = CLLocationCoordinate2DMake([object.latitude的doubleValue],[object.longitude的doubleValue]);
        dispatch_async(dispatch_get_main_queue(),^ {
           [_mapView addAnnotation:customAnnotation];
        });
     }
  });
}

这是一个很好的教程:<一href=\"http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial\"相对=nofollow> GCD和线程

I have alot of annotations to be added to a mkmapview. When i add the annotations, the app freezes for a short period of time. I have understand that the main thread is the only thread allowed to add UI to view, if this is true, how to i make this operation not freeze the app?

// in viewdidLoad
for (NSManagedObject *object in requestResults) {
     CustomAnnotation *customAnnotation = [[CustomAnnotation alloc] init];
     customAnnotation.title = object.title;
     customAnnotation.coordinate = CLLocationCoordinate2DMake([object.latitude doubleValue], [object.longitude doubleValue]);
     [_mapView addAnnotation:customAnnotation];
} 
} // end viewdidload

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
// shows the annotation with a custom image
    MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"mapAnnotation"];
    CustomAnnotation *customAnnotation = (id) annotation;
    customPinView.image = [UIImage imageNamed:@"green"];
    return customPinView;
}

解决方案

You can use Grand Central Dispatch - GCD for doing this.

Try with:

- (void)viewDidLoad
{
  dispatch_async(dispatch_get_global_queue(0, 0), ^{
     for (NSManagedObject *object in requestResults)
     {
        CustomAnnotation *customAnnotation = [[CustomAnnotation alloc] init];
        customAnnotation.title = object.title;
        customAnnotation.coordinate = CLLocationCoordinate2DMake([object.latitude doubleValue], [object.longitude doubleValue]);
        dispatch_async(dispatch_get_main_queue(), ^{
           [_mapView addAnnotation:customAnnotation];
        });
     }
  });
}

This is a nice tutorial: GCD and threading

这篇关于如何添加注释的MKMapView asyncronously?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆