I'm currently fetching a lot of objects that contains both names and coordinates of streets. The returned array has around 22.000 objects and the resulting array we want has around 4000, the rest are duplicates. The problem with this kind of data is that the fetched objects can have the same name but different coordinates, and i'm only interested by getting objects based on unique names. If there are more than one object with the same name, i'll only want to keep the first object.
So far i've been trying to loop through the streets by comparing the names. I'd rather use filter
or some other more performance efficient solution.
My struct
struct StreetName {
var name: String
var polyLine: CLLocationCoordinate2D
}
My code so far
DataManager.shared.getStreetNames { (streets) in
var namesArray: [StreetName] = []
for streetName in streets {
let name = streetName.name
if namesArray.count == 0 {
namesArray.append(streetName)
} else if namesArray.contains(where: {$0.name == name }) {
/* Dont add */
} else {
namesArray.append(streetName)
}
}
self.streetNames = namesArray.sorted(by: {$0.name < $1.name})
self.filteredStreetNames = self.streetNames
OperationQueue.main.addOperation {
self.streetTableView.reloadData()
}
}
This code block works, but runs in around 30 seconds on an iPhone X. Which is way too slow. Any ideas?