问题描述
我的问题是这个
我想显示用于文件下载的ProgressView.由于某种原因,ProgressView会逐渐上升,显示已经下载了多少文件,并且在仍未下载文件时立即填满!我需要修复什么或如何实现它?
I want to show ProgressView for files downloading.For some reason the ProgressView gradually rises, showing how much file is already downloaded , and just immediately fills up when files are still not downloaded! What do I need to fix or how to implement it?
这是我的源代码:
- (IBAction)download:(id)sender {
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
progressSlider.progress = 0.0;
for (i=0;i<=7;i++) {
//Updating progressView and progressLabel
progressLabel.text = [NSString stringWithFormat:@"Загружено: %d из 7",i];
progressView.progress = (float)i/(float)7;
NSString *filePath = [NSString stringWithFormat:@"%@/avto-0-%d.html", [paths objectAtIndex:0],i];
// Download and write to file
NSString *mustUrl = [NSString stringWithFormat:@"http://www.mosgortrans.org/pass3/shedule.php?type=avto&%@", [listOfAvtoUrl objectAtIndex:i]];
NSURL *url = [NSURL URLWithString:mustUrl];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
}
}
推荐答案
我认为它与线程以及主线程如何刷新视图有关.
I think that it is related to threads and how the main thread will refresh the view.
可能会发生两种情况:
调用此方法时,您位于主线程上
You are on the main thread when calling this method
在这种情况下,您不允许主线程执行刷新例程.使用GCD将所有下载内容移至后台队列,然后像pt.2中所述回调主线程.
In such case, you do not allow the main thread to execute refresh routines. Move all this download to a background queue using GCD, and call back the main thread like explained in pt.2.
要将所有内容放入后台队列,您可以致电:
To put everything in a background queue, you could call:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^ {
// your download code goes here
});
或者,您已经在后台线程上,然后使用以下几行来回调主线程:
Or, You are already on the background thread, then use the following lines to call back to the main thread:
dispatch_async(dispatch_get_main_queue(), ^ {
progressView.progress = (float)i/(float)7;
});
最终答案:
- (IBAction)download:(id)sender {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^ {
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
progressSlider.progress = 0.0;
for (i=0;i<=7;i++) {
//Updating progressView and progressLabel
progressLabel.text = [NSString stringWithFormat:@"Загружено: %d из 7",i];
dispatch_async(dispatch_get_main_queue(), ^ {
progressView.progress = (float)i/(float)7;
});
NSString *filePath = [NSString stringWithFormat:@"%@/avto-0-%d.html", [paths objectAtIndex:0],i];
// Download and write to file
NSString *mustUrl = [NSString stringWithFormat:@"http://www.mosgortrans.org/pass3/shedule.php?type=avto&%@", [listOfAvtoUrl objectAtIndex:i]];
NSURL *url = [NSURL URLWithString:mustUrl];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
}
});
}
这篇关于用于在Object-C中下载某些文件的ProgressView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!