1

I have a class that generates images to be printed by the user. These images are created using QuartzCore (and some UIKit elements) and need to be run on the main thread.

In the view visible to the user while the images are being generated, I have a progress bar. This view is the delegate of the class that does the printing, and a method is called by the printer on the view to update the progress bar. The problem is, the progress bar doesn't visibly update until the printer is finished because the printer is clogging up the main thread. I can't move the progress bar off of the main thread because all UI updates must be done on the main thread.

I'm fairly new to multithreading; do I have any other options or must I go with an activity indicator or something similar?

Josh Sherick
  • 2,161
  • 3
  • 20
  • 37

2 Answers2

1

Update your code so that the image creation is done on a background thread. This should be safe.

Then you can make calls onto the main thread to update the progress bar.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Apple engineers like to call this the "box within a box" paradigm. You create a new "box", or queue which contains your heavy lifting. Then, you hop from within the aforementioned queue into the main queue to update the UI. This is very common practice. – Jacob Relkin Mar 08 '13 at 00:24
0

You can use GCD, Raywenderlich Tutorial

- (void)generatePage
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        /*
          Here you can generate page and update progress
          For example:
         */
        [self updateProgress:10.0f];
        sleep(1000);
        [self updateProgress:20.0f];
        sleep(3000);
        [self updateProgress:100.0f];
    });
}

- (void)updateProgress:(float)progress
{
    dispatch_async(dispatch_get_main_queue(), ^{
        progressView.progress = progress;
    });
}
Sergey Kuryanov
  • 6,114
  • 30
  • 52