DispatchWorkItem不会立即取消任务

时间:2019-02-27 13:36:14

标签: arrays swift for-loop dispatch dispatchworkitem

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

有一个包含30000个对象的结构数组和一个textView,在输入字母时,我删除空格并在searchText中设置文本,searchText具有WillSet和DidSet,在WillSet中,我取消所有带有旧文本的搜索任务并开始使用DidSet中添加新文本。在search()函数中,我遍历数组并在anyTranslate()函数中向DispatchWorkItem添加逻辑,当我键入很多字符时,searchText给出错误- [CFString length]: message sent to deallocated instance。我创建了一个变量类型test,该变量类型在anyTranslate()中不应该为true,但有时会打印为true,只有在test等于true时,错误才会始终出现,因为我知道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
        }
    }

}

0 个答案:

没有答案