I have a drawRect method that is pretty slow (100-200 ms). To save time, I need to cache the results. I am doing the actual caching as follows:
// some code to check if caching would be desirable goes here. If it is desirable, then UIGraphicsBeginImageContext(viewSize); CGContextRef c = UIGraphicsGetCurrentContext(); [view.layer renderInContext: c]; UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); [self.cachedImageArray addObject:image]; UIGraphicsEndImageContext();
Caching itself can take up to 40 ms. It is still worth it. But caching has to wait until everything is rendered, or it goes wrong. In addition, caching is a low priority task. Once everything is displayed, it is possible that everything will continue, and if so, caching can wait. But since it uses UIKit, it must be in the main thread.
Instead of introducing some kind of arbitrary delay, is there a bulletproof way to wait this way?
source share