I have this code which is performed each time I click the 'show polygons' button. The problem is that it takes a few seconds to finish running through the code before actually drawing the polygons + adding annotations to map. It actually takes about 10 seconds or so each time.
I am wondering if you can suggest any way to make my code 'more efficient' and speed it up.
firlist
has about 605 lines of data entries and airportfilelines
has almost 9000 lines of data entries.
- (void) updateATC {
for (NSString *firline in firList) {
NSArray *fields = [firline componentsSeparatedByString:@":"];
for (int i = 0; i < atcOnline.count; i++) {
ATC *atc = (ATC *)[atcOnline objectAtIndex:i];
NSArray *callsign = [atc.callsign componentsSeparatedByString:@"_"];
if ([[fields objectAtIndex:0] isEqualToString:[callsign objectAtIndex:0]] && ![atc.callsign containsString:@"_GND"] && ![atc.callsign containsString:@"_TWR"] && ![atc.callsign containsString:@"_DEL"] && ![atc.callsign containsString:@"_DEP"] && ![atc.callsign containsString:@"_APP"] && ![atc.callsign containsString:@"_ATIS"] && ![atc.callsign containsString:@"_SUP"] && ![atc.callsign containsString:@"OBS"]) {
[ctrATC addObject:[atcOnline objectAtIndex:i]];
for (NSString *line in firDisplayLines) {
if ([line hasPrefix:[NSString stringWithFormat:@"DISPLAY_LIST_%@",[fields objectAtIndex:5]]])
{
NSArray *sector = [line componentsSeparatedByString:@"+"];
CLLocationCoordinate2D coordinates[sector.count];
for (int i = 1; i < sector.count; i++) {
NSArray *coordinate = [sector[i] componentsSeparatedByString:@":"];
coordinates[i-1] = CLLocationCoordinate2DMake ([[coordinate objectAtIndex:0] doubleValue],[[coordinate objectAtIndex:1] doubleValue]);
}
MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coordinates count:sector.count-1];
[firOverlays addObject:polygon];
dispatch_async(dispatch_get_main_queue(), ^{
[_mapView addAnnotations:ctrATC];
[_mapView addOverlays:firOverlays];
});
}
}
}
}
}
for (NSString *line in airportFileLines) {
NSArray *fields = [line componentsSeparatedByString:@":"];
for (int i = 0; i < atcOnline.count; i++) {
ATC *atc = (ATC *)[atcOnline objectAtIndex:i];
NSArray *callsign = [atc.callsign componentsSeparatedByString:@"_"];
if ([[fields objectAtIndex:0] isEqualToString: [callsign objectAtIndex:0]] && ![atc.callsign containsString:@"ATIS"] && ![atc.callsign containsString:@"SUP"] && ![atc.callsign containsString:@"OBS"] && ![atc.callsign containsString:@"CTR"] && ![atc.callsign hasPrefix:@"K"])
{
atc.coordinate = CLLocationCoordinate2DMake([[fields objectAtIndex:4] doubleValue],[[fields objectAtIndex:5] doubleValue]);
[otherATC addObject:atc];
} else if ([[[fields objectAtIndex:0] substringFromIndex:1] isEqualToString: [callsign objectAtIndex:0]] && [[fields objectAtIndex:3] isEqualToString:@"US"] && ![atc.callsign containsString:@"ATIS"] && ![atc.callsign containsString:@"SUP"] && ![atc.callsign containsString:@"OBS"] && ![atc.callsign containsString:@"CTR"]) {
atc.coordinate = CLLocationCoordinate2DMake([[fields objectAtIndex:4] doubleValue],[[fields objectAtIndex:5] doubleValue]);
[otherATC addObject:atc];
}
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[_mapView addAnnotations:otherATC];
});
}
A sample line from firList:
EBBU:Brussels:BE:50.4:4.2:100
A sample line from airportFileLines:
KJFK:John F Kennedy:New York:US:40.64:-73.779