mapbox/Simple-KML — Issue #11: Getting exception as “Collection was mutated while being enumerated”
Author: kohdesmond
Created: 2012-01-21 11:23:40+00:00
State: open

Hi Guys,

I tried to check out a user pinpoint with polygon by looping through all the KMLs. the app always crash @ this point:

//create KML in hidden Mapview
-(void)loadKML:(NSMutableArray *)kmlNameArray
{
//dispatch_group_t group = dispatch_group_create();

//remove polygon and redraw again.
[NSThread detachNewThreadSelector: @selector(spinEnd) toTarget:self withObject:nil];

[mapView removeOverlays:mapView.overlays];
[inUserRangeArray removeAllObjects];
[inUserRangeArrayObjectIndex removeAllObjects];
[scrollview removeFromSuperview];
[pageControl removeFromSuperview];
[NSThread detachNewThreadSelector: @selector(spinBegin) toTarget:self withObject:nil];

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];

for (int e=0; e<[kmlNameArray count]; e++)
{
//NSString *kmlNameStr = [kmlNameArray objectAtIndex:e];
Frog *kmlID = [self.fs objectAtIndex:e];
self.kmlID = [NSString stringWithFormat:@"%i",kmlID.fID];
self.kmlIDObjectIndex = [NSString stringWithFormat:@"%i",e];

NSLog(@"asasas %@",kmlIDObjectIndex);

//NSLog(@"KML items %@", kmlNameStr);
//NSLog(@"KML ID %@", kmlID);
//NSLog(@"KML file Path %@",[NSString stringWithFormat:@"%@/data/%@/%@", docDirectory,self.kmlID,[kmlNameArray objectAtIndex:e]]);

SimpleKML *kml = [SimpleKML KMLWithContentsOfFile:[NSString stringWithFormat:@"%@/data/%@/%@", docDirectory,self.kmlID,[kmlNameArray objectAtIndex:e]]error:NULL];

// look for a document feature in it per the KML spec

// dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

if (kml.feature && [kml.feature isKindOfClass:[SimpleKMLDocument class]])
{// see if the document has features of its own
for (SimpleKMLFeature *feature in ((SimpleKMLContainer *)kml.feature).features)
{// otherwise, see if we have any placemark features with a polygon
if ([feature isKindOfClass:[SimpleKMLPlacemark class]] && ((SimpleKMLPlacemark *)feature).polygon)
{
SimpleKMLPolygon *polygon = (SimpleKMLPolygon *)((SimpleKMLPlacemark *)feature).polygon;
SimpleKMLLinearRing *outerRing = polygon.outerBoundary;
//points[i], i = number of coordinates
CLLocationCoordinate2D points[[outerRing.coordinates count]];
NSUInteger i = 0;
for (CLLocation *coordinate in outerRing.coordinates)
{
points[i++] = coordinate.coordinate;
}
// create a polygon annotation for it
self.overlayPolygon = [MKPolygon polygonWithCoordinates:points count:[outerRing.coordinates count]];

//crash here
[mapView addOverlay:overlayPolygon];

// zoom the map to the polygon bounds
[mapView setVisibleMapRect:overlayPolygon.boundingMapRect animated:YES];

}
}
}
}