I have a time consuming process, and have a progress indicator showing the user how far things have gone. Because I have to do the consuming thing ON the main thread, I don’t have the option of simply dispatching an update on the main queue in between updates. I have to switch to a background thread for a moment in order to let the UI update before switching back and continuing.
This is what I have, but it feels very unorthodox. Is there a better way to do “blocks for loops with callbacks to UI” out there that I’m missing? I’m also not entirely sure if this will actually dealloc the block in the end but that’s another story.
__block NSUInteger i = 0;
__block dispatch_block_t obtainBlock;
obtainBlock = [^{
[self obtainAssetAtIndex:i];
progressView.progress += progressPerFile;
i++;
if (i < assets.count) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
usleep(500);
dispatch_async(dispatch_get_main_queue(), obtainBlock);
});
} else {
[self didImportGroup];
[obtainBlock release];
}
} copy];
dispatch_async(dispatch_get_main_queue(), obtainBlock);
Way too complicated thinking. Current setup:
What you want to do:
Here’s the code:
You don’t need a background thread at all (especially when all it’s ever doing is sleeping).