我正在製作一個應用程序,我必須從網絡服務器下載一些文件並將其保存到本地。我有以下JSON
:UIProgressview在下載文件時不會改變
{
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
不動! 有人可以幫助我嗎?
謝謝!
你應該在主線程 – nmh
你必須更新主UI線程執行進度顯示。 –
完成塊是否在主線程上運行?如果是這樣,你有一些工作要做。 – user3386109