Lesson 7: Adding a progress display

In the previous lesson, you added rudimentary synchronization which was done on the main thread. Blocking the main thread in such a way is not recommended. In this lesson you will move the synchronization to a background thread, and add a synchronization observing method to update a progress display.

 Creating the progress toolbar

Synthesize the properties in the implementation and add a release call in the dealloc method. Open the ProgressToolbarViewController by double-clicking ProgressToolbarViewController.xib in the Xcode Resources folder. Since this view will be displayed in a toolbar, you must size it appropriately, and set its background properties:

  1. In the Document window (Command-0), select the View object.

  2. In the Attribute Inspector (Command-1), set the simulated status bar to None.

  3. Set the Background opacity to 0%

  4. Uncheck the Opaque setting.

  5. In the Size Inspector (Command-3), set the width to 232 and the height to 44.

 To add the progress view
  1. Click and drag a UIProgressView from the Library to the View.

  2. In the Size Inspector (Command-3), set the position of the progress view to 26, 29.

  3. Set the width of the progress view to 186.

  4. In the Attributes Inspector (Command-1), set the style to Bar and the progress to zero.

 To add the label
  1. Click and drag a UILabel from the library to the view.

  2. In the Size Inspector (Command-3), set the position of the label to 14, 5.

  3. Set the size of the label to 210, 16.

  4. In the Attribute Inspector (Command-1), set the text to Sync Progress.

  5. Set the layout alignment to center.

  6. Set the font to Helvetica bold, size 12.

  7. Set the text color to white.

  8. Set the shadow color to RGB: (103, 114, 130), with 100% opacity.

 Connect the new label and progress view to the outlets
  1. Select the File's Owner in the Document window.

  2. In the Connectors Inspector (Command-2), link the label outlet to the UILabel you created in the last steps.

  3. Link the progress outlet to the UIProgressView you created in the last steps.

  4. Save the XIB file and close Interface Builder.

This view will be added to the RootViewController's toolbar. However, the DataAccess object will also use a reference to it to display the progress of the synchronization. Add the following instance variable:

ProgressToolbarViewController * progressToolbar;

and add the property to the DataAccess class:

    @property (retain, readwrite) IBOutlet ProgressToolbarViewController *    progressToolbar;

Import the ProgressToolbarViewController header and to synthesize the property in the implementation. To actually add the progress view to the toolbar, add the following to the RootViewController's viewDidLoad method:



    // Create progress display 
    ProgressToolbarViewController *    progress =  
    [[ProgressToolbarViewController alloc]  
     initWithNibName:@"ProgressToolbarViewController"  
     bundle:nil]; 
    
    // Register the toolbar with the DataAccess 
    [[DataAccess sharedInstance] setProgressToolbar:progress]; 
    
    // Setup UIBarButtonItems 
    UIBarButtonItem    *                space =  
    [[UIBarButtonItem alloc] 
     initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  
     target:nil 
     action:nil]; 
    UIBarButtonItem    *                progressButtonItem =  
    [[UIBarButtonItem alloc] initWithCustomView:progress.view]; 
    
    // Put them in the toolbar 
    self.toolbarItems =  
    [NSArray arrayWithObjects:space, progressButtonItem, space, nil]; 
    [space release]; 
    [progressButtonItem release];

The RootViewController now has a progress view in its toolbar, even though the toolbar is hidden. In the next section, you will move synchronization to a background thread and display the progress toolbar during the sync.

 Performing synchronization in a background thread