Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios mkdon';t使用NSThread显示在映射中_Ios_Objective C_Mapkit_Mkannotation_Nsthread - Fatal编程技术网

Ios mkdon';t使用NSThread显示在映射中

Ios mkdon';t使用NSThread显示在映射中,ios,objective-c,mapkit,mkannotation,nsthread,Ios,Objective C,Mapkit,Mkannotation,Nsthread,我有一个在地图中插入注释的方法。此方法由map的委托方法调用 问题是注释不会出现在地图中。我必须再摸一次地图才能显示注释 插入注释后,我使用[CatTransaction flush],但它不起作用 代码上方: 映射委托方法: - (void) mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{ log(@"region changed"); if (_userRegion.center.l

我有一个在地图中插入注释的方法。此方法由map的委托方法调用

问题是注释不会出现在地图中。我必须再摸一次地图才能显示注释

插入注释后,我使用[CatTransaction flush],但它不起作用

代码上方:

映射委托方法:

- (void) mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{ log(@"region changed"); if (_userRegion.center.latitude) { log(@"Move map. New location: %f X %f", self.mapView.centerCoordinate.latitude, self.mapView.centerCoordinate.longitude); NSDictionary *coordinates = @{ @"latitude": [NSString stringWithFormat:@"%f", self.mapView.centerCoordinate.latitude], @"longitude": [NSString stringWithFormat:@"%f", self.mapView.centerCoordinate.longitude] }; NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(updateMap:) object:coordinates]; [thread start]; } if(_userMenuOpened){ [self closeUserMenu]; } if(_settingsMenuOpened){ [self closeSettingsMenu]; } } -(void)地图视图:(MKMapView*)地图视图区域IDChangeAnimated:(BOOL)动画{ 日志(@“区域更改”); if(_userRegion.center.latitude){ 日志(@“移动地图。新位置:%f X%f”,self.mapView.centerCoordinate.latitude,self.mapView.centerCoordinate.longitude); NSDictionary*坐标=@{ @“纬度”:[NSString stringWithFormat:@“%f”,self.mapView.centerCoordinate.latitude], @“经度”:[NSString stringWithFormat:@“%f”,self.mapView.centerCoordinate.longitude] }; NSThread*thread=[[NSThread alloc]initWithTarget:self selector:@selector(updateMap:)object:coordinates]; [线程开始]; } 如果(_userMenuOpened){ [自动关闭用户菜单]; } 如果(_设置未打开){ [自动关闭设置菜单]; } } 插入注释的方法:

- (void) updateMap:(NSDictionary *) coordinates { NSArray *annotationsInMap = [self.mapView annotations]; _busStops = [_ws getStations:10 lat:[[coordinates valueForKey:@"latitude"] doubleValue] lng:[[coordinates valueForKey:@"longitude"] doubleValue]]; for(NSString *stop in _busStops) { BOOL inMap = NO; BPBusStopAnnotation *annotation = [[BPBusStopAnnotation alloc] initWithData:stop]; //Se tiver annotation no mapa verifica se os que vem no WS sao os mesmos if(annotationsInMap.count > 0){ for(BPBusStopAnnotation *pt in annotationsInMap){ if(pt && ![pt isKindOfClass:[MKUserLocation class]]){ if([annotation.codigo isEqual:pt.codigo]){ inMap = YES; break; } } } } if (!inMap) { [self.mapView addAnnotation:annotation]; } } [CATransaction flush]; } -(void)updateMap:(NSDictionary*)坐标{ NSArray*annotationsInMap=[self.mapView annotations]; _公共汽车站=[[u ws getStations:10纬度:[[coordinates valueForKey:@“lation”]doubleValue]lng:[coordinates valueForKey:@“longitude”]doubleValue]; 用于(NSString*停止在_总线停止){ BOOL-inMap=否; BPBusStopAnnotation*annotation=[[BPBusStopAnnotation alloc]initWithData:stop]; //车辆注释无mapa验证车辆无WS sao os mesmos 如果(注释sinmap.count>0){ 用于(注释sinmap中的BPBusStopAnnotation*pt){ if(pt&![pt iskindof类:[MKUserLocation类]]{ if([annotation.codigo isEqual:pt.codigo]){ inMap=是; 打破 } } } } 如果(!inMap){ [self.mapView addAnnotation:annotation]; } } [CATransaction flush]; }
谢谢

是你自己说的:是线。除了主线程,您不能做任何影响接口的事情,这就是这里的问题

(当你在修复这个问题时,如果我是你,我就永远不会使用NSThread。这是所有可能的iOS线程方式中最糟糕的一种。)

在“updateMap”方法中,我改变了

if (!inMap) {
    [self.mapView addAnnotation:annotation];
}
为了


谢谢你的回答@matt,但我该如何解决我的问题呢?我需要在该委托方法中调用ws,但如果我在主线程中这样做,它会冻结屏幕,直到完成ws调用。当你想在后台执行任务时,你会进入后台线程,当你想在主线程中执行任务时,你会回到主线程(例如,更新接口)。它可能会帮助您在继续之前停止并了解iOS中的线程。例如,参见相关的:
[self.mapView performSelectorOnMainThread: @selector(addAnnotation:) withObject: annotation waitUntilDone: YES];