Pangu
Pangu

Reputation: 3819

How to calculate progress of MBProgressHUD mode when used with NSURLConnection?

I am trying to use MBProgressHUD with NSURLConnection, but I'm not sure how to make the animation spin in MBProgressHUDModeDeterminate.

Basically I have this code in viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self fetchFromServer];

    self.HUD = [ [MBProgressHUD alloc] initWithView:self.view];
    self.HUD.labelText = @"Loading Schedule...";
    [self.view addSubview:self.HUD];
    self.HUD.dimBackground = YES;
    [self.HUD show:YES];
}

When this view is loaded, I call a method to fetch from the database. I want MBProgressHUD to load. Then I want it to end once the data has been fetched.

- (void)fetchFromServer
{

    NSURL *url = [[NSURL alloc] initWithString:@"url.com"];

    [NSURLConnection sendAsynchronousRequest:[ [NSURLRequest alloc] initWithURL:url] queue:[ [NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

         NSArray *jsonObj = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding]
                                                                   options:0 error:&error];

             scheduleJSONArray = jsonObj;

             // Progress in HUD should end here after scheduleJSONArray is set

             [self.HUD hide:YES];
      } ];
}

I don't know what to use to update the progress of HUD. Can anyone assist?

Thanks

Upvotes: 1

Views: 1277

Answers (1)

orkenstein
orkenstein

Reputation: 2858

You better use AFNetworking:

MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
// Set up HUD 

__weak ViewController *weakSelf = self;
NSURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:@"url.com" parameters:nil error:nil];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
__weak AFHTTPRequestOperation *weakOperation = operation;
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
  [weakSelf updateHUDForOpetation:weakOperation
                       totalBytes:totalBytesExpectedToRead
                        readBytes:totalBytesRead
                            index:[videoNames indexOfObject:videoName]
                            total:videoNames.count];
}];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  [MBProgressHUD hideHUDForView:self.view animated:YES];
}];

Then update HUD manually. Like this:

- (void)updateHUDTotalBytes:(long long)totalBytes
                  readBytes:(long long)readBytes {
  MBProgressHUD *HUD = [MBProgressHUD HUDForView:self.view];
  HUD.mode = MBProgressHUDModeDeterminate;
  HUD.labelText = [NSString stringWithFormat:@"%lld/%lld", readBytes / 1024, totalBytes / 1024];
  HUD.progress = (double)readBytes / (double)totalBytes;
}

Upvotes: 1

Related Questions