7

I have a serial NSOperationQueue

I have an NSOperation subclass which read a file from disk, create a UIImage from it, and then display the image on the main thread.

Pretending the queue containing about a hundred of NSOperations, what would be the most efficient and graceful solution for such rapid UI updates from the background thread?

Tim
  • 1,877
  • 19
  • 27
  • 1
    Have you tried grabbing a timestamp just before submitting the block to the main thread and then another timestamp at the block's end and comparing the delay? In other words, are you sure it has to do with the submission to the main thread? While you're at it, take a third timestamp after the call to submit the block to measure how long that takes, itself. Also, is your app submitting other work to the main dispatch queue besides the setting of the image to the image view? Perhaps there's a long line in the queue and `NSOperationQueue` has a separate line. – Ken Thomases Aug 08 '15 at 12:18
  • @KenThomases I updated the question. You were on the right direction – Tim Aug 08 '15 at 13:19
  • @Tim after reading your update2 it looks like dispatch_async is faster than NSOperationQueue. So Is the title of your problem wrong? – Sam B Aug 08 '15 at 13:41
  • http://stackoverflow.com/questions/14207036/why-nsoperationqueue-is-faster-than-gcd-or-performselectoronmainthread-when-they – Sam B Aug 08 '15 at 13:41
  • Try using `CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopCommonModes, ^{ ... })`. Also, as a separate experiment, when using `dispatch_async()`, try adding `CFRunLoopWakeUp(CFRunLoopGetCurrent())` to the end of the block. My guess is that `NSOperationQueue` is causing the main run loop to cycle, which causes a UI update pass after each block. I also guess that `dispatch_async()` does not, so the UI doesn't update until something else triggers that. – Ken Thomases Aug 08 '15 at 13:49
  • @SamB Yes. it looks like. I will update the title – Tim Aug 08 '15 at 13:50
  • The usual solution is to call `dispatch_after`. Even a delay of `0` will allow the runloop to cycle (the transaction is committed and drawing takes place). – matt Aug 08 '15 at 15:06
  • This is an interesting observation, but I'm not sure what your question is. –  Aug 08 '15 at 15:18
  • 1
    SO isn't a blog or forum for posting info like this. This is a question and answer site. Since this info could be useful, you should redo it as a question and an answer so it fits the SO format. Otherwise this is likely to be closed and lost. – rmaddy Aug 08 '15 at 19:15
  • As written now, the question would be closed as being far too broad. BTW - I can't simply unblock your question. The best I can do is vote to reopen it. But then 4 others would also need to do so. – rmaddy Aug 11 '15 at 14:59

0 Answers0