UIProgressview在下载文件时不会改变

时间:2014-04-24 06:58:12

标签: ios objective-c uiprogressview

我正在创建一个应用程序,我必须从网络服务器下载一些文件并将其保存在本地。我有以下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",
...
]
}

我所做的是将此字符串值保存到NSArray,然后我将下载其中的每一个。

-(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];

}

这就是我LOG的一部分:

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

但由于某些原因,我的UIProgressview没有移动! 有人可以帮我这个吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

确保更新主线程上的进度视图:

 -(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];
    });
  }