0

I am using an instance of MKLocalSearchCompleter to get string predictions whenever the text in my search bar has changed. I run into one problem when I type too fast into the search bar or when I hold my thumb on the backspace key - I get the following error: The operation couldn’t be completed. (MKErrorDomain error 2.)

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
       if !searchCompleter.isSearching{
            searchCompleter.region = (delegate?.searchResultTableViewControllerNeedsUpdatedMapRegion(self))!
            searchCompleter.queryFragment = searchText
        }
    }

I am assuming just like reverse geocoding, with MKLocalSearchCompleter there is quota on request calls that can happen at any given time. If this is the case, can someone point out a work around...If not can you please explain to me what the problem seems to be?

I print out the list of predicted strings in the console and on the 5th line you can see the error.

fast food

fast food

fast food

Westfield San Francisco Centre

The operation couldn’t be completed. (MKErrorDomain error 2.)

Hilton Hotel

HP Pavilion at San Jose

HPS Mechanical

HP Pavilion at San Jose

Hilton Hotel

House of Prime Rib

hospital

hospital

fja
  • 1,823
  • 1
  • 15
  • 28
  • Are you letting the prior search finish before initiating the next search? Or, if not, are you canceling prior search before initiating the next one? As the [docs](https://developer.apple.com/documentation/mapkit/mklocalsearch/1452652-start) say, "You should call this method only once to start the search operation. Calling this method while the search is running does not stop the original search operation from finishing. However, for each subsequent call, the search object executes your completion handler and passes an error object to it." – Rob Jul 31 '17 at 16:13
  • I use the `if !searchCompleter.isSearching{ }` block in the `searchBar` delegate method to see if there is a search in progress; however, I do not explicitly cancel in progress searches anywhere in my code. @Rob – fja Jul 31 '17 at 16:17
  • Can you set a breakpoint on where you get the error and then go to the Xcode thread inspector? It looks like it's forking into a background thread with each request and you're exhausting the threads and so if I'm right the thread inspector should have hundreds of threads when it errors – Allison Jul 31 '17 at 16:34
  • @Sirens how can I tell if I have exhausted the threads? – fja Jul 31 '17 at 17:20
  • Did you do what I said? If there are hundreds of threads in the thread debugger you've exhausted your threads. – Allison Jul 31 '17 at 18:58
  • Did you find your solution? – Allison Aug 06 '17 at 01:58

0 Answers0