[英]Updating UIProgressView within dispatch_async
I have my app making a google API call using dispatch_async and getting the JSON results that I am parsing through. 我的应用程序使用dispatch_async进行了Google API调用,并获取了我正在解析的JSON结果。 I want to implement a progress view such that it updates depending on how many results from JSON response that I have parsed through.
我想实现一个进度视图,以便它根据我解析过的JSON响应的结果数量进行更新。
So my dispatch_async Google API call is: 所以我的dispatch_async Google API调用是:
dispatch_async(myQueue, ^{
NSData* data1 = [NSData dataWithContentsOfURL:googleURL];
[self performSelectorOnMainThread:@selector(fetchResultsForGoogle:) withObject:data1 waitUntilDone:YES];
});
My fetchResultsForGoogle: withObject:
method parses through the results, and I want to display the progress view on the screen with the number of processed results. 我的
fetchResultsForGoogle: withObject:
方法解析结果,我想在屏幕上显示进度视图以及已处理结果的数量。 So within the fetchResultsForGoogle...
method, I want to have: 因此,在
fetchResultsForGoogle...
方法中,我希望具有:
float percentage = (float)counter/(float)totalItems;
self.progressView.progress = percentage;
NSString *percentText = [NSString stringWithFormat:@"%f %% %@", percentage, @" complete"];
self.progressViewLabel.text = percentText;
But I think understand that when the dispatch_async is performing on another thread, you can't update view on the main thread without using dispatch_async(dispatch_get_main_queue()
. In an attempt to solve this, I tried two ways of implementing this (shown below) but either of these ways don't work for me ( progressView
and progressViewLabel
don't update at all). 但是我认为可以理解的是,当dispatch_async在另一个线程上执行时,如果不使用
dispatch_async(dispatch_get_main_queue()
,就无法更新主线程上的视图dispatch_async(dispatch_get_main_queue()
为了解决这个问题,我尝试了两种方法来实现(如下所示)但是这两种方式对我都不起作用( progressView
和progressViewLabel
根本不会更新)。
Plan A: 计划A:
dispatch_async(myQueue, ^{
NSData* data1 = [NSData dataWithContentsOfURL:googleURL];
[self performSelectorOnMainThread:@selector(fetchResultsForGoogle:) withObject:data1 waitUntilDone:YES];
dispatch_async(dispatch_get_main_queue(), ^{
float percentage = (float)counter/(float)totalItems;
self.progressView.progress = percentage;
NSString *percentText = [NSString stringWithFormat:@"%f %% %@", percentage, @"complete"];
NSLog(@"Percentage: %@", percentText);
self.progressViewLabel.text = percentText;
});
});
Plan B: 方案B:
dispatch_async(myQueue, ^{
NSData* data1 = [NSData dataWithContentsOfURL:googleURL];
[self performSelectorOnMainThread:@selector(fetchResultsForGoogle:) withObject:data1 waitUntilDone:YES];
});
And within fetchResultsForGoogle...
method: 在
fetchResultsForGoogle...
方法中:
dispatch_async(dispatch_get_main_queue(), ^{
float percentage = (float)counter/(float)totalItems;
self.progressView.progress = percentage;
NSString *percentText = [NSString stringWithFormat:@"%f %% %@", percentage, @"complete"];
NSLog(@"Percentage: %@", percentText);
self.progressViewLabel.text = percentText;
});
So any ideas or tips regarding the correct way to implement this is very much appreciated! 因此,非常感谢有关实现此方法的正确方法的任何想法或技巧!
EDIT-SOLVED IT I fixed it. 编辑解决它,我修复了它。 In
dispatch_async
block, I was passing it to performSelectorOnMainThread
. 在
dispatch_async
块中,我将其传递给performSelectorOnMainThread
。 Therefore when I was trying to update the progress view in performSelectorOnMainThread
, the UI won't update until the dispatch_async
was completed. 因此,当我尝试更新
performSelectorOnMainThread
的进度视图时,UI将不会更新,直到dispatch_async
完成。
So, I changed it to performSelectorInBackgroundThread
inside the dispatch_async
block and now the performSelectorOnMainThread
updates the UI like the way it should. 因此,我将其更改为
dispatch_async
块内的performSelectorInBackgroundThread
,现在performSelectorOnMainThread
像应该那样更新了UI。
I am new to all this, but I am glad I am still learning new things. 我不熟悉这一切,但很高兴我仍在学习新事物。
You're right you can't update things in the main thread in the way you're doing it. 没错,您无法按照自己的方式更新主线程中的内容。 You have to perform a selector on the main thread like so:
您必须在主线程上执行选择器,如下所示:
I'm not exactly sure where your percentage changes, but add the line below there. 我不确定您的百分比会在哪里变化,但是请在下面添加一行。 And it'll update your label and progressView on the main thread
它将在主线程上更新标签和progressView
[self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:percentage] waitUntilDone:NO];
-(void) updateProgress:(NSNumber *) num {
float percentage = [num floatValue];
self.progressView.progress = percentage;
NSString *percentText = [NSString stringWithFormat:@"%f %% %@", percentage, @" complete"];
self.progressViewLabel.text = percentText;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.