2014-04-24 29 views
0

我正在製作一個應用程序,我必須從網絡服務器下載一些文件並將其保存到本地。我有以下JSONUIProgressview在下載文件時不會改變

{ 
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不動! 有人可以幫助我嗎?

謝謝!

+0

你應該在主線程 – nmh

+0

你必須更新主UI線程執行進度顯示。 –

+0

完成塊是否在主線程上運行?如果是這樣,你有一些工作要做。 – user3386109

回答

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

它仍然保持不變?進度視圖位於自定義單元格內。上面的代碼也在自定義單元格的自定義類中?也許有問題? – Steaphann

+0

只是爲了節省一方; progressView不是沒有對不對? ;-) – diederikh