7

I'm working on a problem where I have to download around 10 different large files in a queue, and I need to display a progress bar indicating the status of the total transfer. I have this working just fine with ASIHTTPRequest in iOS4, but I'm trying to transition to AFNetworking since ASIHTTPRequest has issues in iOS5 and is no longer maintained.

I know you can report progress on individual requests using AFHTTPRequestOperation's downloadProgressBlock, but I can't seem to find a way to report overall progress of multiple requests that would be executed on the same NSOperationQueue.

Any suggestions? Thanks!

Alex Zielenski
  • 3,591
  • 1
  • 26
  • 44
BBonifield
  • 4,983
  • 19
  • 36

3 Answers3

1
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
    NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];

Operation is AFHTTPRequestOperation

Melvin
  • 3,421
  • 2
  • 37
  • 41
0

I would try subclassing UIProgressView with a subclass that keeps track of all the different items you are watching and then has logic that adds the progress of them all together.

With code like this perhaps:

 @implementation customUIProgressView

-(void) updateItem:(int) itemNum ToPercent:(NSNumber *) percentDoneOnItem {
  [self.progressQueue  itemAtIndexPath:itemNum] = percentDoneOnItem;

  [self updateProgress];
}
-(void) updateProgress {
  float tempProgress = 0;
  for (int i=1; i <= [self.progressQueue count]; i++) {
    tempProgress += [[self.progressQueue  itemAtIndexPath:itemNum] floatValue];
  }
  self.progress = tempProgress / [self.progressQueue count];
}
brainjam
  • 18,863
  • 8
  • 57
  • 82
clreina
  • 41
  • 4
  • This is bad coding practice for MVC design patterns. – Alex Zielenski Dec 03 '11 at 17:21
  • Perhaps. It is the right tool for the job for dynamically sized content. It will accurately show progress without needing to know the size of the content first. Finding the size of large files (such as videos or photos) can be an expensive operation. – clreina Dec 15 '11 at 20:31
0

You can subclass AFURLConnectionOperation to have 2 new properties: (NSInteger)totalBytesSent, and (NSInteger)totalBytesExpectedToSend. You should set these properties in the NSURLConnection callback like so:

- (void)connection:(NSURLConnection *)__unused connection 
   didSendBodyData:(NSInteger)bytesWritten 
 totalBytesWritten:(NSInteger)totalBytesWritten 
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
    [super connection: connection didSendBodyData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite];
    self.totalBytesSent = totalBytesWritten;
    self.totalBytesExpectedToSend = totalBytesExpectedToSend;
}

Your uploadProgress block may look like this:

……(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
    NSInteger queueTotalExpected = 0;
    NSInteger queueTotalSent     = 0;
    for (AFURLConnectionOperation *operation in self.operationQueue) {
        queueTotalExpected += operation.totalBytesExpectedToSend;
        queueTotalSent     += operation.totalBytesSent;
    }
    self.totalProgress = (double)queueTotalSent/(double)queueTotalExpected;
}];
Alex Zielenski
  • 3,591
  • 1
  • 26
  • 44