I have a JSON file for all airports in the world that I am trying to execute in a function, but it is very slow and I am trying to improve its performance. There are 9,500 entries in this JSON file (I would use the web API but could not find a free one, so I use this JSON file). My array is like:
var data = [Dictionary<String, AnyObject>]
Here's what one of the dictionaries looks like:
[DisplayText: YYZ, airportObject: {
0 = 193;
Altitude = 569;
City = Toronto;
Country = Canada;
DST = A;
IATA = YYZ;
ICAO = CYYZ;
Latitude = "43.677223";
Longitude = "-79.630556";
Name = "Lester B Pearson Intl";
TZ = "America/Toronto";
UTC = "-5";
}, DisplaySubText: Lester B Pearson Intl]
The goal is to have TextField autocomplete with the name of the airport that the user enters into the field. I wrote a function to filter these entries using user input. It works, however it is very slow and takes about 1 second for each letter entered, and the processor prints up to 50%.
Here is the function
func applyFilterWithSearchQuery(filter : String) -> [Dictionary<String, AnyObject>]
{
var lower = (filter as NSString).lowercaseString
var filteredData = data.filter({
if let match : AnyObject = $0["DisplayText"]{
return (match as NSString).lowercaseString.hasPrefix((filter as NSString).lowercaseString)
}
else{
return false
}
})
return filteredData
}
How to improve the performance of this feature?