如何在异步线程中更新ProgressView [英] How To Update ProgressView in Async Thread

查看:68
本文介绍了如何在异步线程中更新ProgressView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS中,我对GCD和队列的组合几乎没有疑问.让我把那些放在这里

I am having few doubts about Combinations of GCD and queues, in iOS. let me put those here

首先.根据IOS6编程指南,这是用于使用异步和同步下载图像的小代码段.及其如下

First. According to IOS6 programming cookbook, here is small code snippet for downloading images using async and sync. and its as below

dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);dispatch_async (concurrentQueue, ^ {
UIImage *image = nil;
dispatch_sync(concurrentQueue, ^ { /* download image here */ });
dispatch_sync(dispatch_get_main_queue, ^ { /* show the downloaded image here */ });
});

第二.我需要从网址下载几张图片,同时我需要更新进度视图以显示已下载了多少张图片.所以我正在使用以下代码段

Second. I need to download few images from Url, meanwhile i need to update the progress view showing how many images have got downloaded. so i am using following code snippet

dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async (concurrentQueue, ^ { dispatch_sync(dispatch_get_main_queue, ^ { /* download image here and update progress view here */ });
});

请注意,我正在更新主线程上的进度视图.但是,在下载所有图像之后,将显示我的进度视图.我检查了其他链接,但答案不是很清楚.没有得到问题的原因.我的代码如下.

kindly note that i am updating progress view on main thread. But my progress view is getting displayed after all images has been downloaded. i checked other links but was not pretty clear with the answers.not getting the reason for the problem. My code is as below.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{dispatch_sync(dispatch_get_main_queue(), ^{
       alert = [[UIAlertView alloc] initWithTitle:@"Please Wait Downloading reports..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] ;
       prgView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
       prgView.frame = CGRectMake(10, 50, 270, 20);
       [alert addSubview:prgView];
       [alert show];
      prgView.progress=0.0;
       actual= 0.0;

       for(NSInteger i=0; i<[fileLinks count];i++)
       {

        //  [self downLoadFileFromUrl : fileLinks[i]: i ];
           NSLog(@"Url is %@", fileLinks[i]);
           NSLog(@"i value is %d", i);
           NSData *imagedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileLinks[i]]];
           NSString * _filename=  [fileLinks[i] lastPathComponent];
           NSMutableString *filePath = [[NSMutableString alloc] initWithFormat:@"%@", [VSCore getTempFolder]];
           [filePath appendString: _filename] ;
           [imagedata writeToFile: filePath atomically:YES];
           prgView.progress =((float) i /([fileLinks count]));
           NSLog(@" progress value is %f", prgView.progress);

       }
   });
});

那么这是什么问题?我的另一个疑问是,如果有人在执行长时间的处理任务时想要抓住用户(显示进度视图或活动指示器),那么他们是否可以在主线程中这样做呢?(前提是它们显示进度条或活动指示器),为什么我们必须使用异步,与并发线程或主线程同步的组合等.

so what is problem here? and my other doubt is, if any one wants to hold the user (displaying progress view or activity indicator) while performing long processing task, can't they do that in main thread ? (provided they are displaying progress bar or activity indicator), why we have to use combination of async, sync with concurrent thread or main thread etc etc.

我有点困惑,在什么情况下我必须使用异步,同步,主队列,并发队列,串行队列的适当组合.如果有人给我解释或提供一个很好的链接供我理解async,sync和3队列的组合.那就太好了.

and i am bit confused with, for what scenario i had to use the proper combination of async, sync, main queue , concurrent queue, serial queue. if any one give explains me or provide me good link for understand The combinations of async , sync and 3 queue. that will be great.

推荐答案

如果仔细看一下代码,您会发现实际上是在主线程中运行所有程序.

If you take a closer look at your code you'll notice you're actually running everything in the main thread.

^{dispatch_sync(dispatch_get_main_queue(), ^{
       alert = [[UIAlertView alloc] initWithTitle:@"Please Wait Downloading reports..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] ;
       prgView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
       prgView.frame = CGRectMake(10, 50, 270, 20);
       [alert addSubview:prgView];
       [alert show];
      prgView.progress=0.0;
       actual= 0.0;
       for(NSInteger i=0; i<[fileLinks count];i++)
       {

        //  [self downLoadFileFromUrl : fileLinks[i]: i ];
           NSLog(@"Url is %@", fileLinks[i]);
           NSLog(@"i value is %d", i);
           NSData *imagedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileLinks[i]]];
           NSString * _filename=  [fileLinks[i] lastPathComponent];
           NSMutableString *filePath = [[NSMutableString alloc] initWithFormat:@"%@", [VSCore getTempFolder]];
           [filePath appendString: _filename] ;
           [imagedata writeToFile: filePath atomically:YES];
           prgView.progress =((float) i /([fileLinks count]));
           NSLog(@" progress value is %f", prgView.progress);

       }
   });

这将在主线程中运行downloadFileFromUrl和进度视图的更新(除非downloadFileFromUrl是异步操作,我不知道)

This is going to run the downloadFileFromUrl and the update of the progress view in the main thread (unless the downloadFileFromUrl is an async operations, which I don't know)

基本上,您需要在辅助线程中运行downloadFileFromUrl方法,然后仅在主线程上运行此位:prgView.progress =((float)i/([fileLinks count]));

Basically you need to run the downloadFileFromUrl method in a secondary thread, then only run this bit on the main thread: prgView.progress =((float) i /([fileLinks count]));

这篇关于如何在异步线程中更新ProgressView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆