DispatchWorkItem do not cancel task at once

361 views Asked by At

I have a nasty search problem.

There is an array of structures with 30000 objects and a textView, when entering letters, I delete spaces and set the text in searchText, searchText has WillSet and DidSet, in WillSet I cancel all search tasks with old text and start searching with new text in DidSet. in the search() function, I go through the array and add logic to the DispatchWorkItem in the anyTranslate() function, the searchText gives the error - [CFString length]: message sent to deallocated instance when I type a lot of characters. I created a variable type test which should not be true in anyTranslate() but it sometimes prints true, the error always appears only if test equals true, as I understand DispatchWorkItem does not cancel all tasks at once,

how can I solve this problem?

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

There are 0 answers