Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
How to decide if a statement needs to be placed in DispatchQueue.main.async?

I am trying to get a subview's bound size in viewDidLoad: (the canvas.bounds)

Initially I did not use the DispatchQueue.main.async wrapper, and the size is not returned correctly. So by experiment, I wrapped the statement in main thread queue. Then it worked.

I know there is a guideline saying that "UI related operation needs to be placed in main thread". But how does this translates into actual coding rule of thumb:

  1. If I am just querying a UI property, like getting bounds size, do I need to wrap it inside main thread queue?
  2. If I am changing a UI property, e.g. change the bounds size, do I need to wrap it inside the main thread queue? (likely yes I guess)

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    
    // stack views
    view.addSubview(photoView)
    view.addSubview(canvas)
    
    DispatchQueue.main.async {
        self.canvas.startPoint = CGPoint.zero
        self.canvas.endPoint = CGPoint(x: self.canvas.bounds.width / 2.0, y: self.canvas.bounds.height)
    
        self.canvas.setNeedsDisplay()
    }
    

Actually this leads to another question I want to ask naturally: having to wrap code in dispatch main looks “unclean”. If u have to wrap it , doesn’t it mean ‘viewDidLoad’ is not the correct life cycle to use? There should be a more appropriate life cycle that doesn’t require dispatch main wrapper?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your issue is that viewDidLoad is not the correct place to get the size of anything. It's too soon.

Using DispatchQueue.main.async simply delays that code just enough that the size has been updated (maybe).

The proper solution is to get the size in the proper place. Use either viewDidAppear or viewDidLayoutSubviews.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!