3
votes

I have a method that inserts annotations in the map. This method is called by a delegate method of map.

The problem is that the annotations don't appear in map. I have to touch the map one more time to show the annotations.

After inserting the annotations, I'm using [CATRansaction flush], but it don't work.

Above the code:

Map delegated method:

- (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];
    }

}

Method that inserts the annotation:

- (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];

}

Thank's!!!

2

2 Answers

2
votes

In "updateMap" method, I changed

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

for

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

You said it yourself: it's the thread. You mustn't do anything that affects the interface on anything but the main thread, and that's the problem here.

(And while you are fixing this, if I were you, I would never use NSThread. It is the worst of all possible ways to do iOS threading.)