Steaphann
Steaphann

Reputation: 2777

UIProgressview not changing while downloading files

I am making an app where I must download some files from a webserver and save it locally. I have the following JSON:

{
status: 200,
message: "OK",
files: [
"magazines/dentalasia/DentalAsia1/images/toc-thumb-dummy.jpg",
"magazines/dentalasia/DentalAsia1/images/bg-grid-iphone.png",
"magazines/dentalasia/DentalAsia1/images/cover/toc-thumb.jpg",
"magazines/dentalasia/DentalAsia1/images/cover/cover-typo.png",
...
]
}

What I do is I save this string values into an NSArray and then I'm going to download each one of them.

-(IBAction)downloadMagazine:(id)sender{
    // 1
    progressView.hidden = NO;

    NSArray *splitArray = [mag.mag_folder componentsSeparatedByString:@"/"];
    NSString *magName = [splitArray lastObject];

    NSString *string = [NSString stringWithFormat:@"%@/webservice/magazine/get-book/apikey/%@/magazine/%@/name/%@",baseUrl,apikey,magazine,magName];
    NSLog(@"STRING IS %@",string);
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // 2
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        //Create general directory

        NSDictionary *JSON = [responseObject copy];
        NSArray *files = [JSON valueForKey:@"files"];
        reversedFiles = [[files reverseObjectEnumerator] allObjects];

        amountFiles = reversedFiles.count;

        NSArray *splitArray = [mag.mag_folder componentsSeparatedByString:@"/"];
        NSString *magName = [splitArray lastObject];

        NSString *rootString = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:magName];
        NSError *error = nil;
        if (![[NSFileManager defaultManager] fileExistsAtPath:rootString])
            [[NSFileManager defaultManager] createDirectoryAtPath:rootString withIntermediateDirectories:NO attributes:nil error:&error];
        rootString = [rootString stringByAppendingPathComponent:@"/www"];
        if (![[NSFileManager defaultManager] fileExistsAtPath:rootString])
            [[NSFileManager defaultManager] createDirectoryAtPath:rootString withIntermediateDirectories:NO attributes:nil error:&error];


        for(int i = 0 ; i<reversedFiles.count ; i++){
            NSString *file = [reversedFiles objectAtIndex:i];
            NSString *strUrl = [NSString stringWithFormat:@"%@/%@",baseUrl,file];
            NSArray *splitUrl = [file componentsSeparatedByString:@"/"];
            NSString *lastObject = [splitUrl lastObject];
            if ([lastObject rangeOfString:@"."].location == NSNotFound) {
                rootString = nil;
                rootString = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:magName];
                rootString = [rootString stringByAppendingPathComponent:@"/www"];
                for (int i=0; i<splitUrl.count; i++) {
                    if(i>2){
                        NSString *pathComponent = [NSString stringWithFormat:@"/%@",[splitUrl objectAtIndex:i]];
                        rootString = [rootString stringByAppendingPathComponent:pathComponent];
                    }
                }
                if (![[NSFileManager defaultManager] fileExistsAtPath:rootString])
                    [self calculatePie:i];
                [[NSFileManager defaultManager] createDirectoryAtPath:rootString withIntermediateDirectories:NO attributes:nil error:&error];
            }else{
                NSURL *url = [NSURL URLWithString:strUrl];
                NSData *data = [NSData dataWithContentsOfURL:url];
                if(data)
                {
                    rootString = nil;
                    rootString = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:magName];
                    rootString = [rootString stringByAppendingPathComponent:@"/www"];
                    for (int i=0; i<splitUrl.count; i++) {
                        if(i>2){
                            NSString *pathComponent = [NSString stringWithFormat:@"/%@",[splitUrl objectAtIndex:i]];
                            rootString = [rootString stringByAppendingPathComponent:pathComponent];
                        }
                    }
                    [data writeToFile:rootString atomically:YES];
                    [self calculatePie:i];
                }
            }
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        // 4
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving magazine"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }];
    // 5
    [operation start];
}


-(void)calculatePie:(int)i{
    float progress = (100.0 / amountFiles) * i;
    float progress2 = progress / 100.0;
    NSLog(@"Progress is %f",progress2);
    [progressView setProgress:progress2 animated:YES];

}

And this is how a part of my LOG looks like:

2014-04-24 08:56:18.507 DentalAsia[47301:60b] Progress is 0.340136
2014-04-24 08:56:18.764 DentalAsia[47301:60b] Progress is 0.343537
2014-04-24 08:56:19.041 DentalAsia[47301:60b] Progress is 0.346939
2014-04-24 08:56:19.210 DentalAsia[47301:60b] Progress is 0.350340
2014-04-24 08:56:19.549 DentalAsia[47301:60b] Progress is 0.353741
2014-04-24 08:56:19.714 DentalAsia[47301:60b] Progress is 0.357143

But for some reason my UIProgressview is not moving! Can someone help me with this ?

Thanks !

Upvotes: 0

Views: 139

Answers (1)

diederikh
diederikh

Reputation: 25281

Make sure you update the progress view on the main thread:

 -(void)calculatePie:(int)i{
    float progress = (100.0 / amountFiles) * i;
    float progress2 = progress / 100.0;
    NSLog(@"Progress is %f",progress2);

    dispatch_async(dispatch_get_main_queue(), ^{
        [progressView setProgress:progress2 animated:YES];
    });
  }

Upvotes: 1

Related Questions