为什么在进行本地搜索时会收到MKErrorDomain错误? [英] Why do I get an MKErrorDomain error when doing a local search?

查看:169
本文介绍了为什么在进行本地搜索时会收到MKErrorDomain错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个搜索栏,用户可以在其中输入字符串并搜索地址或公司.

I am trying to implement a search bar where the user can type in a string and search for an address or a business.

要查找企业,我使用Yelp API来外包我所需的信息.

To find businesses I use Yelp APIs to outsource the information I need.

要查找地址,我使用Apple的MKLocalSearch API来获取所需的信息.

To find addresses I use Apple's MKLocalSearch APIs to get the information I need.

但是,我确实有问题.当我按住退格按钮从搜索栏中清除文本或在搜索栏中键入太快时,出现MKErrorDomain错误:

However, I do have a problem. When I hold on to the backspace button to clear out the text from the search bar or type too fast into the search bar I get an MKErrorDomain error:

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

出现此错误时,我将需要稍等片刻才能使代码再次运行并从API中检索信息.

When I get this error I will have to wait a few moments in order for the code to work again and retrieve information from the APIs.

下面的代码是我要实现的目标:

The following code is what I have to implement what I am looking for:

这是搜索栏委托方法:

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchBar.text == "" {
        addServiceCellToTableView()
        loadSearchHistory()
        return
    } else if searchBar.text != "" {
        removeServiceCellFromTableView()
    }


    if searchCompleter.isSearching{
        searchCompleter.cancel()
        searchCompleter.region = (delegate?.businessSearchResultTableViewControllerNeedsUpdatedMapRegion(self))!
        searchCompleter.queryFragment = searchText
    } else {
        searchCompleter.region = (delegate?.businessSearchResultTableViewControllerNeedsUpdatedMapRegion(self))!
        searchCompleter.queryFragment = searchText
    }

}

我使用MKLocalSearchCompleter根据用户在搜索栏中输入的内容获取建议:

I use MKLocalSearchCompleter to get suggestions based on what the user types into the search bar:

func completerDidUpdateResults(_ completer: MKLocalSearchCompleter){

    guard completer.results.count != 0 else {return}

    var searchTerm: String = completer.results.first!.title
    if completer.results.first!.subtitle != "" {
        searchTerm = searchTerm + ", " + completer.results.first!.subtitle
    }


    if let _ = addressDetector.firstMatch(in: searchTerm, options: [], range: NSMakeRange(0, searchTerm.utf8.count)){
        searchAddress(for: searchTerm)
    } else {
        getBusinesses(withSearchTerm: searchTerm, userCoordinates: currentUserLocation.coordinate)
    }


}

在上面的代码中,我使用NSDataDetector来查看建议的文本是否为地址...如果是,我将其输入MKLocalSearch ...

In the code above I use NSDataDetector to see if the suggested text is an address...If so I feed it into MKLocalSearch...

最后,为了搜索地址,我定义了一种称为searchAddress(for:)的方法:

Finally, In order to search for for addresses I defined a method called searchAddress(for:):

func searchAddress(for string: String){

        let localSearchRequest = MKLocalSearchRequest()
        localSearchRequest.naturalLanguageQuery = string
        localSearchRequest.region = (delegate?.businessSearchResultTableViewControllerNeedsUpdatedMapRegion(self))!

        let localSearch = MKLocalSearch(request: localSearchRequest)
        localSearch.start(completionHandler: {searchResponse, error in
            guard error == nil else {
                print(error.debugDescription)
                return
            }

            guard let mapItems = searchResponse?.mapItems else {return}

            self.tableViewDataSourceList = mapItems
            self.tableView.reloadData()
            self.delegate?.businessSearchResultTableViewStopedGettingBusiness(self, with: self.tableViewDataSourceList, at: CLLocationCoordinate2D(latitude: self.currentUserLocation.coordinate.latitude, longitude: self.currentUserLocation.coordinate.longitude))
        })
    }

当我键入太快或按住退格键时,在控制台中出现以下错误:

When I type too fast or hold on the backspace key I get the following error in the console:

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

任何帮助将不胜感激:-)

Any help will be much appreciated :-)

推荐答案

在这里看到的是MKError.loadingThrottled错误.您将不得不延迟发送给Apple的请求.

What you're seeing here is the MKError.loadingThrottled error. You will have to delay requests you're sending to Apple.

您可以通过在用户每次更新搜索查询时重新启动计时器来做到这一点.您可以通过延长计时器的长度来调整ping API的频率.通过在每次更新查询时重置计时器,可以避免在字符快速变化时发送多个请求.

You can do this by restarting a timer every time the user updates the search query. You can adjust how frequently you ping the API by extending the length of the timer. By resetting the timer each time the query is updated, you avoid sending multiple requests when the characters are changing rapidly.

// declare and store Timer somewhere

func searchAddress(with query: String) {

}

func timerDidFire(_ sender: Any) {
    let query = textField.text
    searchAddress(with: query)
}

Timer.scheduledTimerWithTimeInterval(0.5, target: self, selector: #selector(timerDidFire), userInfo: nil, repeats: false)

这篇关于为什么在进行本地搜索时会收到MKErrorDomain错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆