我正在從互聯網下載zip文件並將其解壓縮。這個過程需要15秒左右。我想顯示給用戶的某種時間。作爲開始,我只想在下載完成並開始解壓縮時更新我的_countUpView
UIView
上的UILabel
。我相信,我需要對持有UILabel
,但我似乎無法找出什麼我做錯了的視圖中使用setNeedsDisplay
,這裏是我的代碼:如何使用setNeedsDisplay方法與代碼同步更新UILabel
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://URLtothiszipfile>/all.zip"]];
NSData *slicedata= [NSData dataWithContentsOfURL:url];
NSError *error;
NSString *isignsPath = [NSString stringWithFormat:@"%@/iSigns",docsDir];
if (![filemgr fileExistsAtPath:isignsPath]){ //Does directory already exist?
if (![filemgr createDirectoryAtPath:isignsPath withIntermediateDirectories:NO attributes:nil error:&error]){
NSLog(@"Create directory error: %@", error);
}
}
thePathAndName= [NSString stringWithFormat:@"%@/iSigns/all.zip", docsDir];
[slicedata writeToFile:thePathAndName atomically:YES];
_countUpLab.text= @"Unzipping";
[self displayCountUpNOW];
NSLog(@"Unzipping");
[SSZipArchive unzipFileAtPath:thePathAndName toDestination:isignsPath overwrite:true password:@"" error:&error];
if (error.code!= noErr){
NSLog(@"Unzip error");
return;
}else{
NSLog(@"Unzipped successfully");
}
而且setNeedsDisplay
方法是:
- (void) displayCountUpNOW {
dispatch_async(dispatch_get_main_queue(), ^{
[_countUpView setNeedsDisplay];
});
}
我將標籤設置爲「解壓縮」大約10秒後,我的標籤不會更改爲「解壓縮」,直到NSLog
顯示「解壓成功」。
在這10秒鐘內,解壓縮發生,但是我想用來更新標籤的計時器每秒都會停止執行,所以我無法在標籤中顯示流逝的時間。
請幫我理解這個異步環境。
卡門
EDIT-EDIT-EDIT 下面的代碼似乎異步工作,我甚至有我的經時指示器工作,因爲我的定時器未停止。我無法在SSZipArchive中找到沒有文件的方法,因此我將保存文件保存在其中。我如何做鄧肯?再次感謝,這非常漂亮!
還有一個問題:什麼是最好的方式來知道什麼時候異步請求仍然未完成,通過設置一個全局標誌變量,當請求時,並在異步過程完成時清除它?
gotALL= 0;
_countUpLab.text= @"Downloading";
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://URLtothiszipfile>/all.zip"]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *slicedata, NSError *error1){
if ([slicedata length]> 0 && error1== nil){
NSString *isignsPath = [NSString stringWithFormat:@"%@/iSigns",docsDir];
if (![filemgr fileExistsAtPath:isignsPath]){ //Does directory already exist?
NSError *error2;
if (![filemgr createDirectoryAtPath:isignsPath withIntermediateDirectories:NO attributes:nil error: &error2]){
NSLog(@"Create directory error: %@", error2);
[self endCountUp];
return;
}
}
thePathAndName= [NSString stringWithFormat:@"%@/iSigns/all.zip", docsDir];
[slicedata writeToFile:thePathAndName atomically:YES];
_countUpLab.text= @"Unzipping";
NSLog(@"Unzipping");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error3;
[SSZipArchive unzipFileAtPath:thePathAndName toDestination:isignsPath overwrite:true password:@"" error:&error3];
dispatch_async(dispatch_get_main_queue(), ^(void) {
if (error3.code!= noErr){
NSLog(@"Unzip error %@", error3.description);
[self endCountUp];
return;
}else{
NSLog(@"Unzipped successfully");
gotALL= 1;
[self endCountUp];
return;
}
});
});
}else if ([slicedata length]== 0 && error1== nil){
//todo yet
}else if (error1!= nil){
//todo yet
}
}];
試着移動'_countUpLab.text = @「解壓縮」;'dispatch_async塊內部 – sha
好想法,但似乎沒有工作。謝謝卡門 – Carm100