简体   繁体   English

使用GCD更新UIProgressView - 正确的时间 - iPhone

[英]Updating UIProgressView using GCD - Correct Timing - iPhone

I have a UIProgress view which I want to update as an NSXML Parser runs. 我有一个UIProgress视图,我想在NSXML Parser运行时更新。 The NSXML parser is running on the main thread and takes about 30 seconds to run. NSXML解析器在主线程上运行,运行大约需要30秒。

At the end of a parsing section I call a method in my app delegate : 在解析部分的末尾,我在我的app delegate中调用一个方法:

[appDelegate updateLoadingScreen:0.75];

The app delegate then calls the modelLoadingProgressUpdate method in the loadingScreen : 然后,app委托调用loadingScreen中的modelLoadingProgressUpdate方法:

- (void)updateLoadingScreen : (float)progress {
NSLog(@"PROGRESS REQUEST !!!");

NSNumber * progressNS = [NSNumber numberWithFloat:progress];

[loadingScreen modelLoadingProgressUpdate : progressNS];

}

The modelLoadingProgressUpdate method then uses a dispatch queue to update the UIProgressBar : 然后,modelLoadingProgressUpdate方法使用调度队列来更新UIProgressBar:

- (void) modelLoadingProgressUpdate :(NSNumber *)progress {

dispatch_queue_t mainqueue = dispatch_get_main_queue();
dispatch_async(mainqueue, ^  { 

float updateProgress = [progress floatValue];
NSLog(@"Update Progress is %f", updateProgress);

progressView.progress = updateProgress;

});

}

However nothing is updating on my UIProgressView. 但是我的UIProgressView上没有更新任何内容。 The modelLoadingProgressUpdate seems to only trigger once the parser has completed. modelLoadingProgressUpdate似乎只在解析器完成后才会触发。

Can anyone spot what is wrong ? 谁能发现什么是错的?

Here's your problem: 这是你的问题:

The NSXML parser is running on the main thread and takes about 30 seconds to run. NSXML解析器在主线程上运行,运行大约需要30秒。

So your NSXMLParser is blocking the main thread, so the block that you dispatch isn't being executed until the NSXMLParser is done. 因此,您的NSXMLParser正在阻塞主线程,因此在NSXMLParser完成之前,您发送的块不会被执行。

Since the NSXMLParser is running on the main thread anyway, you could just get rid of the dispatch_async and just update progressView.progress directly in modelLoadingProgressUpdate: . 由于NSXMLParser无论如何都在主线程上运行,你可以直接删除dispatch_async并直接在modelLoadingProgressUpdate:更新progressView.progress modelLoadingProgressUpdate: .

Or, you can run the NSXMLParser on a background thread, in which case it won't block the main thread and the progress updating block will run soon after you dispatch it. 或者,您可以在后台线程上运行NSXMLParser,在这种情况下,它不会阻止主线程,并且在您发送后很快就会运行进度更新块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM