0

I have the following code. It worked fine in Swift 2.3:

 self.newsScrollView.contentSize = CGSize(width: self.newsScrollView.frame.size.width * CGFloat(topics.count), height: self.newsScrollView.frame.size.height)
    for i in 0..<topics.count {
        let frame = CGRect(x: CGFloat(i) * self.newsScrollView.frame.size.width, y: 0, width: self.newsScrollView.frame.size.width, height: self.newsScrollView.frame.size.height)
        let topic = topics[i]
        let newsView = TrendingTopicView(topic: topic, frame: frame)
        newsView.delegate = self
        self.newsScrollView.addSubview(newsView)
    }

Now, I receive a warning in the debugger:

  This application is modifying the autolayout engine from a background thread after 
  the engine was accessed from the main thread.
  This can lead to engine corruption and weird crashes.

So, I tried editing it to this:

 DispatchQueue.main.async {
     self.newsScrollView.addSubview(newsView)  
 }  

But I still receive the warning. I have also tried the DispatchQueue.main.async(execute:) as well. What am I doing wrong? If I'm not mistaken, the DispatchQueue.main.async inSwift 3 is the equivalent of dispatch_async(dispatch_get_main_queue) in Swift 2.x right?

Any help is appreciated. Thanks

jjjjjjjj
  • 4,203
  • 11
  • 53
  • 72
  • You're looking too narrowly. The code you've posted doesn't show anything running on a background thread but if that's where you're crashing then it is definitely being called by something that is. Find all places where you hop on a background thread and make sure nothing down the call chain modifies the UI. – par Nov 11 '16 at 10:28
  • http://stackoverflow.com/questions/37805885/how-to-create-dispatch-queue-in-swift-3 – Sanju Nov 11 '16 at 10:40
  • DispatchQueue.main.async (execute: { () -> Void in }) – Sanju Nov 11 '16 at 10:47
  • i've tried that as well @sanju ju – jjjjjjjj Nov 11 '16 at 15:50
  • refer to this http://stackoverflow.com/a/28321213/6521116 – LF00 May 17 '17 at 07:56

0 Answers0