2

I have a custom view within my ViewController that I want to update some info on. This info is being drawn asynchronously from Firebase.

I'm trying to use view.setNeedsDisplay or view.setNeedsLayout to update the view. But since each only changes the view after the next drawing cycle, how can I force the next drawing cycle to happen without forcing the user to leave the ViewController and come back?

jsanabria
  • 451
  • 6
  • 8
  • There is a drawing cycle as soon as your code finishes. There is no need to leave anything. – matt Apr 03 '16 at 22:02

1 Answers1

2

Calling setNeedsDisplay or setNeedsLayout is sufficient to trigger “the next drawing cycle”, if you call it on the main thread. It sounds like you might not be calling it on the main thread. If you're not sure which thread you're calling it on, dispatch back to the main queue:

dispatch_async(dispatch_get_main_queue()) {
    self.someView.setNeedsDisplay()
}
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • The information in the view still isn't updating. Might their be another method to dump and recall the view? Such as a function that is called on a button tap. – jsanabria Apr 04 '16 at 02:56
  • You're going to have to provide some code. Edit your question. – rob mayoff Apr 04 '16 at 04:21
  • "Calling setNeedsDisplay or setNeedsLayout is sufficient to trigger “the next drawing cycle”, I don't think this is true. – Ron Feb 02 '18 at 21:42
  • It is true. Adding a block to the main queue wakes up the main run loop (if it was waiting for an event), runs the block, and then calls observers before waiting for the next event. Core Animation registers a before-waiting observer. That Core Animation observer runs the layout and drawing phases. You can look at [this answer](https://stackoverflow.com/a/15168471/77567) for my analysis of the run loop and how Core Animation hooks into it. – rob mayoff Feb 02 '18 at 22:32
  • If you're having trouble getting your view to be redisplayed, you should post your own question with details. – rob mayoff Feb 02 '18 at 22:34
  • I had the same issue, and this solution worked, BUT... the correct code is now DispatchQueue.main.async { self.someView.setNeedsDisplay() } at least, that worked for me :o) – JonH Apr 08 '22 at 18:05