2014-01-10 2 views
1

지도에 특수 효과를 삽입하는 메소드가 있습니다. 이 메소드는 map의 delegate 메소드에 의해 호출됩니다.MKAnnotation이 NSThread를 사용하여지도에 나타나지 않습니다.

문제는 주석이지도에 나타나지 않는다는 것입니다. 특수 효과를 표시하려면지도를 한 번 더 터치해야합니다.

주석을 삽입 한 후 [CATRansaction 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) 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]; 

} 

감사의!

+1

관련 : http://stackoverflow.com/questions/1995245/iphone-mapview-interrupted 답안 @m에 대한 – Anna

답변

1

, I를 대한

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

을 변경

[self.mapView performSelectorOnMainThread: @selector(addAnnotation:) withObject: annotation waitUntilDone: YES]; 
1

당신은 스스로 말했습니다. 그것은 실입니다. 주 스레드 이외의 인터페이스에 영향을주는 작업을해서는 안되며 여기가 문제입니다.

(그리고 당신은이 문제를 해결하는 동안, 당신, 내가 것 결코 사용 NSThread. 그것은 아이폰 OS가 스레딩 할 수있는 모든 가능한 방법 중 최악이다.라면) "updateMap"방법에서

+0

감사의 att,하지만 어떻게 내 문제를 해결할 수 있습니까? 위임 메서드에서 ws를 호출해야하지만 주 스레드에서 해당 호출을 수행하면 ws 호출을 완료 할 때까지 화면이 정지됩니다. –

+1

백그라운드에서 작업을 수행 할 때 백그라운드 스레드로 들어가고 기본 스레드에서 물건을 수행하려는 경우 (예 : 인터페이스 업데이트) 기본 스레드로 되돌아갑니다. – matt

+1

더 나아 가기 전에 iOS에서 스레딩을 중단하고 배우는 데 도움이 될 수 있습니다. 예를 들어, [나의 책의 쓰레드] 장 (http://www.apeth.com/iOSBook/ch38.html)을보십시오. – matt

관련 문제