0

I got a SearchBar that giving the name it print the name searched in a TableView. Before add the key searched I am checking if my Database got the variable. If my database got it I add the searched word in the TableView. My problem is that at the moment matchingItems or response.mapItems got double variable or more and it print a lot of times the same name in the TableView. I have tried a lot of time to fix that but I don't know how do it.

Image of bug > http://i67.tinypic.com/2jfyxdf.png Example of MKMapItem

<MKMapItem: 0x6000003566e0> {
isCurrentLocation = 0;
name = "Arco di Traiano";
placemark = "Arco di Traiano, Via Traiano, 82100 Benevento, Italia @ <+41.13253257,+14.77915406> +/- 0.00m, region CLCircularRegion (identifier:'<+41.13253316,+14.77915406> radius 1414.16', center:<+41.13253316,+14.77915406>, radius:1414.16m)";
timeZone = "Europe/Rome (CEST) offset 7200 (Daylight)";
url = "http://www.comune.benevento.it/bn2_pagine/_mediagallery/pid.php?id=11";
}

The code it this:

var matchingItems: [MKMapItem] = []

extension LocationSearchTable : UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {

        if searchController.searchBar.text == nil || (searchController.searchBar.text?.count)! < 1 {
            self.matchingItems.removeAll()
            self.tableView.reloadData()
        }

        guard let mapView = mapView,
            let searchBarText = searchController.searchBar.text else { return }

        let request = MKLocalSearchRequest()
        request.naturalLanguageQuery = searchBarText
        request.region = mapView.region
        let search = MKLocalSearch(request: request)

        search.start { response, _ in
            guard let response = response else {
                return
            }
            for (index , name) in response.mapItems.enumerated() {

            if (checkIfDatabaseGotThis(key: String(name.name!)) != nil){
                self.matchingItems.append(response.mapItems[index])
                self.tableView.reloadData()
            }

        }
    }
}
}
  • Can you show an example of the data with your duplicate values? Also, what do you consider to be a duplicate? Do you want to have unique names, locations, ids? – donnywals Jun 12 '18 at 08:48
  • I want have unique names, I put an image of bug, wait –  Jun 12 '18 at 08:51
  • Please post the actual data, you are working with, an image won't help to find the solution. Seeing an example of the `MKMapItems` in your console will. – donnywals Jun 12 '18 at 08:52
  • I have added it –  Jun 12 '18 at 08:56

1 Answers1

0

Updated so the sample deduplicates by name:

var seenNames = Set<String>()
for (index , name) in response.mapItems.enumerated() {
    let item = response.mapItems[index]
    if(checkIfDatabaseGotThis(key: String(name.name!)) != nil && !seenNames.contains(item.name)){
        self.matchingItems.append(item)
        seenNames.insert(item.name)
        self.tableView.reloadData()
    }
}

That should remove all duplicates from your list of items based on the name. It keeps track of all the existing names that you have seen. If the name hasn't been seen before, the item is added to the list. Otherwise it is ignored.

donnywals
  • 7,241
  • 1
  • 19
  • 27
  • I have tried in this way but it's not deleting the duplicate :( I think that its not working because the duplicate got a different Hashable value like > First duplicate and the second –  Jun 12 '18 at 09:00
  • I see, then what exactly do you consider a duplicate? Because different hash values would mean that the item does not have the same metadata. (that `0x60800034ce40` part isn't the hash, it's the memory address) – donnywals Jun 12 '18 at 09:06
  • I mean that I don't want item that got same name, if you see the screenshot that I put in the main post you'll see what I mean. Thanks in advance for your time –  Jun 12 '18 at 09:07
  • I have updated this answer to deduplicate based on. the item name – donnywals Jun 12 '18 at 09:13
  • Code is working if you declare seenNames as global variable! Thank you! –  Jun 12 '18 at 09:38
  • Awesome! :) Please mark this answer as correct so others can find it if they face a similar issue – donnywals Jun 12 '18 at 09:39
  • Its create a new bug. If you try to search a name it will search successful and it's working fine. Example: If you got some name similar to this like "Name1: Test, Name2: Testing" it will show Test and Testing in the tableView. If you click on Test name , it search it but if you try to research it for two time it will show only Test name and will erase Testing name. –  Jun 12 '18 at 21:52