ios – Why does UISearchBar textual content discipline enter stall, despite the fact that delegate textDidChange func returns rapidly?


I’ve a UISearchBarDelegate textDidChange handler that does database lookups which can take a number of seconds to finish.

The issue is, after I kind textual content into the search bar, as soon as it will get to the third character enter, e.g. the purpose the place database lookup is initiated, search bar textual content enter hangs – i.e. it will not echo a brand new character till desk outcomes are reloaded! At that time the iOS soft-keyboard momentarily reveals an expanded view of the final key typed for a number of seconds (usually it’s displayed just for a fraction of a sec whereas typing as person suggestions point out which keys are pressed). However on this case the indicator hangs longer than acceptable, whereas enter is blocked (queued up).

That is baffling, as a result of I put the database lookup in its personal (asynchronous) process and AFAIK database lookup ought to not be hanging the primary thread. Thus, I feel I ought to be capable to kind characters rapidly and easily into the search bar, and database outcomes will seem independently when obtainable. I used debug print statements to verify the delegate methodology returns earlier than the tableView reload known as, implying that certainly the Activity just isn’t stalling the primary thread.

I will additionally point out it’s my assumption that as quickly as I return from the delegate methodology that the textField ought to let new characters echo instantly… as a result of the primary thread just isn’t tied to database delays.

What is perhaps going incorrect?

    //
    // MARK: UISearchBarDelegate strategies
    //
    extension SearchForItViewController : UISearchBarDelegate {
        
        func searchBar(_ searchBar: UISearchBar,  textDidChange searchText: String) {
            if searchType == .stuff {
                someStuff.removeAll(keepingCapacity: true)
            } else {
                someOtherStuff.removeAll(keepingCapacity: true)
            }                
            if searchText.depend == 0 {
                self.tableView.reloadData()
                searchBar.searchTextField.font = UIFont.italicSystemFont(ofSize: 14)
                return
            } else if searchText.depend < 3 {
                return
            }
            searchBar.searchTextField.font = UIFont.systemFont(ofSize: 14)

            let matchText = searchText.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).capitalized
                        
            let matchTextLo = matchText
            let matchTextHi = matchText + "z"
            
            var someStuffTemporary : [Stuff]! = nil
            Activity {
                change searchScope {
                case .international:
                    do {
                        someStuffTemporary  =  strive boringStuff(matchTextLo, matchTextHi, restrict: 50, offset: 0)
                        someStuffTemporary  += strive coolStuff(matchTextLo, matchTextHi, restrict: 50, offset: 0)
                    } catch { print("Error fetching issues for search time period "(matchText)"") }

                case let .otherStuffSpecific(_, stuffIdentifier):
                    if stuffId == "otherStuff" {
                        do {
                            stuffListTemporary = strive otherStuff(matchTextLo, matchTextHi, restrict: 50, offset: 0)
                        } catch { print("Error fetching issues from otherStuff. Search time period "(matchText)"") }
                    } else {
                        do {
                            stuffListTemporary = strive strangeStuff(matchTextLo, matchTextHi,
                                                                       otherStuffCode: stuffId, restrict: 50, offset: 0)
                        } catch { print("Error fetching issues from strangeStuff. Search time period "(matchText)"") }
                    }
                }
                self.stuffList = stuffListTemporary
                tableView.reloadData()
            }
        }
    }