1

I am in a process of moving my app to IOS 7.
I have a map and on that map I draw MKPolyLine.
Everything worked until IOS 7 now app crash.
I have changed viewForOverLay with new method:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolyline *route = overlay;
        MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
        routeRenderer.strokeColor = [UIColor redColor];
        routeRenderer.lineWidth = 7;
        return routeRenderer;
    }
    else return nil;
}

In ViewDidLoad I call:

[self performSelectorInBackground:@selector(drawPathInBackground) withObject:nil];

And this is the implementation:

-(void)drawPathInBackground{
for(int idx = 0; idx < [routes count]; idx++)
    {
        Path *m_p = [routes objectAtIndex:idx];
        CLLocationCoordinate2D workingCoordinate;
        workingCoordinate.latitude=m_p.Latitude;
        workingCoordinate.longitude=m_p.Longitude;
        MKMapPoint point = MKMapPointForCoordinate(workingCoordinate);
        pointArr[idx] = point;
    }
    self.routeLine = [MKPolyline polylineWithPoints:pointArr count:[routes count]];
    //[self.mapView addOverlay:self.routeLine];
    //free(pointArr);
    dispatch_async(dispatch_get_main_queue(), ^{
    [self.mapView addOverlay:self.routeLine];
    free(pointArr);
});
}

On this line: [self.mapView addOverlay:self.routeLine]; I get: EXC_BAD_ACCESS(code = 2, address = 0x0)

1110
  • 7,829
  • 55
  • 176
  • 334

1 Answers1

7

You are not supposed to do ANY UI operations on a background thread. UI on the main thread ONLY.

graver
  • 15,183
  • 4
  • 46
  • 62
  • Use something like _dispatch_async(dispatch_get_main_queue(), ^{ // your UI operations });_ to dispatch your UI operations in the main thread. – Tobias Sep 26 '13 at 10:15
  • I have changed -(void)drawPathInBackground{... in my question (end of the function) is that the right way to make this? – 1110 Sep 26 '13 at 10:22
  • @1110 Don't store the produced MKPolyline in a property. The problem is that this introduces a race condition. Just use a local variable for it (the block captures the object). – Nikolai Ruhe Sep 26 '13 at 10:40