【问题标题】:ProgressView for Downloading some files in Object-CProgressView 用于在 Object-C 中下载一些文件
【发布时间】:2012-11-22 12:33:40
【问题描述】:

我的问题是这样的:

我想在文件下载时显示 ProgressView。由于某种原因,ProgressView 逐渐上升,显示已经下载了多少文件,当文件仍未下载时立即填满!我需要解决什么问题或如何实施?

这是我的源代码:

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

【问题讨论】:

  • 两个问题:这是在主线程上完成的吗?它可能是阻止主线程刷新,或者如果没有在主线程上完成,那么您对进度的调用应该在主线程上调度。另外,尝试使用 setProgress:animated: 而不是简单地分配一个值。
  • @Resh32 是的,在主线程上!以及如何在主线程上发送进度调用?
  • @Resh32 setProgress: 动画:在下载文件结束时顺利移动到最后,但只有在加载所有文件之后!
  • 好的,然后我修改了我的答案以在后台队列上完成所有工作,并且只调用主队列来刷新进度视图。试试吧
  • 这需要 iOS 5 BTW。

标签: objective-c progress-bar uiprogressview download


【解决方案1】:

我认为这与线程以及主线程如何刷新视图有关。

可能会出现两种情况:

调用此方法时你在主线程上

在这种情况下,您不允许主线程执行刷新例程。使用 GCD 将所有这些下载移动到后台队列,并按照 pt.2 中的说明回调主线程。

要将所有内容放在后台队列中,您可以调用:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^ {
        // your download code goes here


    });

或者,你已经在后台线程,那么使用下面这几行回调到主线程:

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

【讨论】:

  • 不客气。在进行 UI 工作时,始终要牢记主运行循环。
猜你喜欢
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多