为什么在进行本地搜索时会出现MKErrorDomain错误?

时间:2017-12-07 06:33:00

标签: ios swift mapkit mklocalsearch

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

要查找商家,我使用Yelp API外包我需要的信息。

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

但是,我确实遇到了问题。当我按住退格按钮以清除搜索栏中的文本或在搜索栏中输入太快时,我收到MKErrorDomain错误:

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

当我收到此错误时,我将不得不等待一段时间才能让代码再次运行并从API中检索信息。

以下代码是我必须实现的目标:

这是搜索栏委托方法:

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根据用户在搜索栏中输入的内容获取建议:

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 ...

最后,为了搜索地址,我定义了一个名为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))
        })
    }

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

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.)

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

1 个答案:

答案 0 :(得分:2)

您在此处看到的是MKError.loadingThrottled错误。您将不得不延迟向Apple发送的请求。

您可以通过每次用户更新搜索查询时重新启动计时器来执行此操作。您可以通过延长计时器的长度来调整ping API的频率。通过在每次更新查询时重置计时器,可以避免在字符快速更改时发送多个请求。

// 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)