Search code examples
iosswiftautolayoutgrand-central-dispatchnsoperationqueue

How to use correctly OperationQueue?


This question is relative to this thread: how-to-set-the-height-of-a-cell-depending-on-a-uilabel-with-a-typewriter-effect

In my tableViewController, my cell contains UILabel with a typewriter effect managed with setTextWithTypeAnimation;

func configureCell(tableView: UITableView, cell: ParagraphTableViewCell, atIndexPath indexPath: IndexPath) {

    let paragraph = paragraphArray[indexPath.row] as! Paragraph
    cell.paragraph = paragraph

        self.queue = OperationQueue()

        let operation1 = BlockOperation(block: {

            cell.dialogueLabel.setTextWithTypeAnimation(typedText: paragraph.dialogueLabel.text!, queue:self.queue, callBackAfterCharacterInsertion: {

                self.tableView.beginUpdates()
                self.tableView.endUpdates()
            })

        })


    operation1.completionBlock = {

    cell.buttonsStackViewHeightConstraint.constant = CGFloat(HEIGHT_CONSTRAINT)

    UIView.animate(withDuration: 0.3, animations: {
        cell.contentView.layoutIfNeeded()
    }, completion: nil)

    }

    queue.addOperation(operation1)

   }

My typewriter is inside a UILabel extension :

extension UILabel {

func setTextWithTypeAnimation(typedText: String, queue: OperationQueue, characterInterval: TimeInterval = 0.05, callBackAfterCharacterInsertion:(()->())?) {

    text = ""

    for (_, character) in typedText.characters.enumerated() {

        if queue.isSuspended {

            OperationQueue.main.isSuspended = true
            OperationQueue.main.cancelAllOperations()
            break;
        }

        OperationQueue.main.addOperation {

            self.text = self.text! + String(character)
            callBackAfterCharacterInsertion?()

        }

        Thread.sleep(forTimeInterval: characterInterval)
    }
}
}

First I used DispatchQueue to manage the animation inside a cell (see how-to-set-the-height-of-a-cell-depending-on-a-uilabel-with-a-typewriter-effect), but I needed to stop the thread when user closes the view controller. That's why I'm using OperationQueue (DispatchQueue cannot be stopped)

@IBAction func closeViewController(sender: AnyObject) {

    dismiss(animated: true, completion: nil)

        if self.queue != nil {

            self.queue.isSuspended = true
            self.queue.cancelAllOperations()
            self.queue = nil
    }
}

The issue is when completionBlock is called, the app crashes when I try to update a layout constraint.

This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread.

How can avoid this crash?


Solution

  • You must call UI stuff on the mainQueue.

    Example:

    operation1.completionBlock = {
        DispatchQueue.main.async {
            cell.buttonsStackViewHeightConstraint.constant = CGFloat(HEIGHT_CONSTRAINT)
    
            UIView.animate(withDuration: 0.3, animations: {
                cell.contentView.layoutIfNeeded()
            }, completion: nil)
        }
    }