The original question ...............................................
If you are an advanced drawRect user, you will find out that, of course, drawRect will not actually work until “all processing is complete”.
setNeedsDisplay places the view as invalid and the OS, and basically waits until all processing has been completed. This can be infuriating in the general situation in which you want to:
- view controller 1
- runs some function 2
- which step by step 3
- creates increasingly complex images and 4
- at each step you setNeedsDisplay (wrong!) 5
- until all work is done 6
Of course, when you do the above 1-6, all that happens is that drawRect only starts once after step 6.
Your goal is to update the view at point 5. What to do?
The solution to the original question ..............................................
In a word, you can (A) the background of the big picture, and call to the forefront for updating the user interface or (B) there may be debatable there are four “immediate” methods that do not use the background process. For the result, run the demo program. It has #defines for all five methods.
A truly startling alternative solution introduced by Tom Swift ..................
Tom Swift explained the amazing idea of a fairly simple
manipulation of the execution cycle . Here's how you run the run loop:
[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate date]];
This is truly an amazing piece of technology. Of course, special care should be taken when manipulating the launch cycle, and many of them pointed out this approach strictly for experts.
An unusual problem that occurs ..........
Although a number of methods work, they actually do not work, because there is an unusual artifact of progressive slowdown that you will see in the demo.
Scroll to the “answer” that was inserted below, showing the console output - you can see how it gradually slows down.
Here's a new SO question:
Mysterious "gradual slowdown" problem in run loop / drawRect
Here is the V2 demo application ...
http://www.fileswap.com/dl/p8lU3gAi/stepwiseDrawingV2.zip.html
You will see that he tests all five methods,
#ifdef TOMSWIFTMETHOD [self setNeedsDisplay]; [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate date]]; #endif #ifdef HOTPAW [self setNeedsDisplay]; [CATransaction flush]; #endif #ifdef LLOYDMETHOD [CATransaction begin]; [self setNeedsDisplay]; [CATransaction commit]; #endif #ifdef DDLONG [self setNeedsDisplay]; [[self layer] displayIfNeeded]; #endif #ifdef BACKGROUNDMETHOD
You can see for yourself which methods work and which don't.
you can see the weird "progressive slowdown." Why is this happening?
You can see the controversial TOMSWIFT method, there are virtually no problems with responsiveness. Click to reply at any time. (but still the bizarre problem of "gradual slowdown")
Thus, the overwhelming thing is the strange "progressive slowdown": at each iteration, for unknown reasons, the time spent on the loop decreases. Please note that this applies to both the "correct" (background) and the use of one of the "direct" methods.
Practical solutions ........................
For those who read in the future, if you really cannot get this to work in production code because of the “gradual slowdown of secrecy” ... Felz and Void each of them presented amazing solutions in another specific question, I hope that this helps.