Search code examples
objective-casihttprequestuiprogressview

persist the state of progress bar


I am using ASIHttpRequest to download documents in my app.I am displaying the progessbar for each document downloaded.The documents are displayed in a UITableView,so there is a separate progressbar for each document cell in the table.When the user clicks on 'Back' and then comes to the document table again,I want to display the progress bar.But it is not displayed.The code for downloading the document is as follows:

- (void)startDownloadingDocumentUsingIndexPath:(NSIndexPath*)indexPath 
{   
    Document *document = [self.objects objectAtIndex:indexPath.row];
    self.ip=indexPath;
    if(isNetworkAvailable)
    {   
        NSString *str = [NSString stringWithFormat:kBody_convertDocToByte,
                         document.docCatId,
                         document.docCatName,
                         document.docChksum,
                         document.docLocation,
                         document.docName,
                         document.docSize,
                         document.docType,
                         document.docUpdated,
                         document.docUpdatedBy,
                         document.documentId
                         ];
        NSMutableData *data = [NSMutableData dataWithData:[str dataUsingEncoding:NSUTF8StringEncoding]];
        self.req = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:kURL]];

        self.req.shouldAttemptPersistentConnection   = NO;
        [self.req setDownloadProgressDelegate:_progress];

        self.req.delegate=APP_DEL;
        self.req.timeOutSeconds=timeOut;
        [self.req setDidFinishSelector:@selector(didFinishDownloadDocument:)];
        [self.req setDidFailSelector:@selector(didFailDownloadDocument:)];
        [self.req addRequestHeader:@"SOAPAction" value:kAction_convertDocToByte];
        [self.req addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
        [self.req setPostBody:data];
        [self.req setRequestMethod:@"POST"];
        [self.req setContentLength:[data length]];

        self.req.userInfo=[NSDictionary dictionaryWithObjectsAndKeys:document,@"data",self.objCategory.categoryName,@"catname",self,@"vctr", nil];
        [[APP_DEL nq] addOperation:self.req];
        [[APP_DEL nq] go];
        [APP_DEL nq].showAccurateProgress = YES;
    } 
    else {
        UIAlertView *av=[[UIAlertView alloc] initWithTitle:@"Message" message:@"Please connect to internet & try downloading/updating document later." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [av show];
    }
}

What changes should I make in the code so that the state of the progress bar is persisted?

EDIT:- The document table is actually a 'child' table.It is displayed when the user selects a row in the 'parent' table.The code in 'parent' table is:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.nxt_obj_STMaster2ViewController=[[STMaster2ViewController alloc] initWithNibName:@"STMaster2ViewController" bundle:nil];
    self.nxt_obj_STMaster2ViewController.objCategory=[self.objects objectAtIndex:indexPath.row];
    self.nxt_obj_STMaster2ViewController.managedObjectContext=self.managedObjectContext;
    self.nxt_obj_STMaster2ViewController.detailViewController=self.detailViewController;
    self.nxt_obj_STMaster2ViewController.pre_STMasterViewController=self;

    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    [self.navigationController pushViewController:self.nxt_obj_STMaster2ViewController animated:YES];
}

The viewDidLoad for the 'child' document view controller looks like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(button_refreshTapped:)];

    self.navigationItem.rightBarButtonItem=item;

    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"left-background.jpg"]];
    [tempImageView setFrame:self.tableView.frame];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    self.tableView.backgroundView = tempImageView;

    [self startLoadingData:self.detailViewController.liveData];
}

Solution

  • So, what you are doing is creating a brand new shiny view controller every time that you call tableView:didSelectRowAtIndexPath:. Instead, you should only create it once and re-display it when needed. I'm not sure of all of your logic though, so you may need to create a new one when changing rows (in which case you should compare your .objCategory as well to see if it is the same as your view controller that you are about to push.)

    Try changing it to this:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (!self.nxt_obj_STMaster2ViewController)
        {
            self.nxt_obj_STMaster2ViewController=[[STMaster2ViewController alloc] initWithNibName:@"STMaster2ViewController" bundle:nil];
            self.nxt_obj_STMaster2ViewController.managedObjectContext=self.managedObjectContext;
            self.nxt_obj_STMaster2ViewController.detailViewController=self.detailViewController;
            self.nxt_obj_STMaster2ViewController.pre_STMasterViewController=self;
        }
    
        self.nxt_obj_STMaster2ViewController.objCategory=[self.objects objectAtIndex:indexPath.row];
    
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
        [self.navigationController pushViewController:self.nxt_obj_STMaster2ViewController animated:YES];
    }