如何异步将注释添加到MKMapView? [英] How to add annotations to MKMapView asynchronously?
本文介绍了如何异步将注释添加到MKMapView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有很多注释要添加到mkmapview中.当我添加注释时,该应用程序会冻结一小段时间.我知道主线程是允许添加UI进行查看的唯一线程,如果属实,我如何使此操作不冻结应用程序?
I have a lot 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;
}
推荐答案
您可以使用尝试:
- (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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文