使用后台线程从网址加载注释。销不动或缩放图形页面展现之前 [英] Loading annotations from url using background thread. Pins doesn't show before moving or scaling mapView

查看:199
本文介绍了使用后台线程从网址加载注释。销不动或缩放图形页面展现之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载使用后台线程从URL注释。之前我移动或缩放图形页面销不显示。如何更新我的看法?

我viewDidAppear

   - (无效)viewDidAppear:(BOOL)动画
{[超级viewDidAppear:动画]//创建线程
[NSThread detachNewThreadSelector:@selector(loadPList)toTarget:自withObject:无];}

loadPList

   - (无效)loadPList {//加载的plist的NSString * urlStr = [[NSString的页头] initWithFormat:@http://www.domain.com/data.xml ];NSURL * URL = [NSURL URLWithString:urlStr]; *的NSDictionary字典= [[NSDictionary的页头] initWithContentsOfURL:URL];NSMutableArray里*注释= [[NSMutableArray里的alloc]初始化];NSArray的* annotationsOnMap = mapView.annotations;如果([annotationsOnMap计数>注释计数]){[图形页面removeAnnotations:annotationsOnMap];}其他{//什么都不做}如果([[NSUserDefaults的standardUserDefaults] boolForKey:@blackKey]){NSArray的*安= [字典objectForKey:@组别];的for(int i = 0; I<安计数];我++){    * NSString的坐标= [[安objectAtIndex:我] objectForKey:@坐标];    双realLatitude = [[[坐标componentsSeparatedByString:@,] objectAtIndex:1]的doubleValue];
    双realLongitude = [[[坐标componentsSeparatedByString:@,] objectAtIndex:0]的doubleValue];    MyAnnotation * myAnnotation = [[MyAnnotation的alloc]初始化];
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = realLatitude;
    theCoordinate.longitude = realLongitude;    myAnnotation.coordinate = CLLocationCoordinate2DMake(realLatitude,realLongitude);    myAnnotation.title = [[安objectAtIndex:我] objectForKey:@名称];
    myAnnotation.subtitle = [[安objectAtIndex:我] objectForKey:@地址];
    myAnnotation.icon = [[安objectAtIndex:0] objectForKey:@图标​​];
    [图形页面addAnnotation:myAnnotation];
    [注解ADDOBJECT:myAnnotation];}
}其他{//什么都不做}//并与其他类别相同....//更新UI dispatch_async(dispatch_get_main_queue()^ {}); }


解决方案

您正在更新从非UI用户界面 - 线程这是不行的。

您必须调用code段的更新UIThread块内你的UI如下:

例如

  [图形页面removeAnnotations:annotationsOnMap];

必须与UI线程调用

  dispatch_async(dispatch_get_main_queue()^ {
        //更新用户界面,如果你要
        [图形页面removeAnnotations:annotationsOnMap];
    });

请注意,您必须调用main_queue线程内所有的UI更新

  dispatch_async(dispatch_get_main_queue()^ {
        //所有UI更新code一定要来这里
    });

I load annotations from url using background thread. Pins doesn't show before I move or scale mapView. How can I update my view?

My viewDidAppear

- (void)viewDidAppear:(BOOL)animated
{

[super viewDidAppear:animated];

//Create the thread 
[NSThread detachNewThreadSelector:@selector(loadPList) toTarget:self withObject:nil];

}

loadPList

- (void) loadPList { //Load the plist NSString *urlStr = [[NSString alloc] initWithFormat:@"http://www.domain.com/data.xml"];

NSURL *url = [NSURL URLWithString:urlStr]; NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfURL:url];

NSMutableArray *annotations = [[NSMutableArray alloc]init];

NSArray *annotationsOnMap = mapView.annotations; if ([annotationsOnMap count] > [annotations count]) { [mapView removeAnnotations:annotationsOnMap];

} else { //Do nothing }

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blackKey"]) {

NSArray *ann = [dict objectForKey:@"Category1"];

for(int i = 0; i < [ann count]; i++) {

    NSString *coordinates = [[ann objectAtIndex:i] objectForKey:@"Coordinates"];

    double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue];
    double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue];

    MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = realLatitude;
    theCoordinate.longitude = realLongitude;

    myAnnotation.coordinate=CLLocationCoordinate2DMake(realLatitude,realLongitude);

    myAnnotation.title = [[ann objectAtIndex:i] objectForKey:@"Name"];
    myAnnotation.subtitle = [[ann objectAtIndex:i] objectForKey:@"Address"];
    myAnnotation.icon = [[ann objectAtIndex:0] objectForKey:@"Icon"];


    [mapView addAnnotation:myAnnotation];
    [annotations addObject:myAnnotation];



}
}

else { //Do nothing }

//And same with other categories....

//Update the ui dispatch_async(dispatch_get_main_queue(), ^{

}); }

解决方案

You are updating the UI from a non UI - Thread this will not work

You will have to call segments of code that update your ui inside the UIThread Block as following:

For example

[mapView removeAnnotations:annotationsOnMap];

must be called in UI-Thread

   dispatch_async(dispatch_get_main_queue(), ^{
        //Update UI if you have to
        [mapView removeAnnotations:annotationsOnMap];
    });

Please note that you have to call all your UI updates inside the main_queue thread

   dispatch_async(dispatch_get_main_queue(), ^{
        //All UI updating code must come here
    });

这篇关于使用后台线程从网址加载注释。销不动或缩放图形页面展现之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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