3

我有一个讨厌的搜索问题。

有一个包含30000个对象和一个textView的结构数组,输入字母时,我删除空格并在searchText中设置文本,searchText有WillSet和DidSet,在WillSet中我取消所有旧文本的搜索任务并开始搜索新文本已设置。在search()函数中,我遍历数组并向函数中的 DispatchWorkItem 添加逻辑,当我输入大量字符时anyTranslate(),searchText 会出错。- [CFString length]: message sent to deallocated instance我创建了一个test不应该为真anyTranslate()但有时打印为真的变量类型,只有当测试等于真时才会出现错误,据我所知DispatchWorkItem不会一次取消所有任务,

我怎么解决这个问题?

static var splitArr = (first:[Words](),second:[Words](),third:[Words](), four:[Words](), five:[Words]()) // fragmented array for faster searching
var work:[DispatchWorkItem?] = [nil,nil,nil,nil,nil]
var test = false

private var searchText = "" {
    didSet{
        test = false
        if searchText.count >= 2 {
            self.search(nil)
        }
    }

    willSet{
        for (i,ind) in self.work.enumerated() {
            ind?.cancel()
            self.test = true
        }
    }
}

func search(_ completion:(() -> ())?){

    update = true
    words.removeAll()
    DispatchQueue.main.async {
        self.tableView.isUserInteractionEnabled = false
        self.indicator.isHidden = false
        self.indicator.startAnimating()
    }

    let arr = [ViewController.splitArr.first, ViewController.splitArr.second, ViewController.splitArr.third, ViewController.splitArr.four, ViewController.splitArr.five]

    for (i,value) in arr.enumerated() {

        work[i] = DispatchWorkItem { [weak self] in
            let lang = Language.first()
            for (index,words) in value.enumerated() {
                guard self?.work[i]?.isCancelled == false else { break }
                self?.anyTranslate(with: words, and: lang)
                if index == value.count - 1 {

                    DispatchQueue.main.async {
                        if self?.words.count == 0 {
                            self?.noFound()
                        }
                        self?.tableView.reloadData()
                        self?.tableView.isUserInteractionEnabled = true
                        self?.indicator.isHidden = false
                        self?.indicator.stopAnimating()
                        completion?()
                    }

                }
            }
        }

        guard let current = work[i] else { return }
        DispatchQueue.global(qos: .userInteractive).async(execute: current)

    }

}

func anyTranslate(with words: Words, and lang:String){

    let array = Language.getLang(lang: lang, word: words)
    for value in array[0] ?? [""] {
        if test == true {
            print(true)
        }
         // - [CFString length]: message sent to deallocated instance
        if value.hasPrefix(searchText) {
            addWord(with: words)
            return
        }
    }

}
4

0 回答 0