onmyway133/MainThreadGuard

Application does not crash when calling setNeedsLayout() on background thread

andriichernenko opened this issue · 1 comments

Executing the following code snippet in the app does not cause crash:

dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) {
  self.collectionView?.setNeedsLayout()
}

Couple of things I checked:

  1. Code is executed
  2. The thread is different from main
  3. self.collectionView is not nil

@deville I use this code, and it asserts me

override func viewDidLoad() {
  super.viewDidLoad()

  let layout = UICollectionViewFlowLayout()
  let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)

  view.addSubview(collectionView)

  dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) {
    collectionView.setNeedsLayout()
  }
}