大佬教程收集整理的这篇文章主要介绍了ios – 如何使用GCD说明后台任务?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
- (IBACtion)signInBttn:(id)sender { MBProgressHUD *hudd = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hudd.mode = MBProgressHUDModeAnnularDeterminate; hudd.labelText = @"Loading"; __block float value = 0; for (int j = 0; j<2000; j++) { dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{ for (int i = 0; i<20000 ; i++) { } value += 0.001; dispatch_async( dispatch_get_main_queue(),^{ hudd.progress = value; }); }); } }
hud完全达到100%.这仅仅是为了我的信息,我不知道如何创建计算某事的后台任务以及何时完成40%的HUD令人耳目一新,达到了40%的进步.我希望自己清楚明白,如果有人有时间帮助改进我的代码,那么非常感谢任何答案
通常这不是必需的(大部分时间我们异步进行的操作都很慢,以至于我们没有问题),但如果做的事情比UI运行得更快,那么你可以创建这是一个“调度源”.我将用UIProgressview来说明它,但同样适用于任何UI:
// create source for which we'll be incremenTing a counter,// and tell it to run the event handler in the main loop // (because we're going to be updating the UI) dispatch_source_t source = dispatch_source_create(DISPATCH_sourcE_TYPE_DATA_ADD,dispatch_get_main_queue()); // specify what you want the even handler to do (i.e. update the HUD or progress bar) dispatch_source_set_event_handler(source,^{ self.iterations += dispatch_source_get_data(sourcE); [self.progressview setProgress: (float) self.iterations / kMaxIterations]; }); // start the dispatch source dispatch_resume(sourcE); // Now,initiate the process that will update the source dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,^{ for (long i = 0; i < kMaxIterations; i++) { // presumably,do something meaningful here // Now increment counter (and the event handler will take care of the UI) dispatch_source_merge_data(source,1); } // when all done,cancel the dispatch source dispatch_source_cancel(sourcE); });
@property (nonatomiC) long iterations;
我将kMaxIterations常量定义如下:
static long const kMaxIterations = 10000000l;
以上是大佬教程为你收集整理的ios – 如何使用GCD说明后台任务?全部内容,希望文章能够帮你解决ios – 如何使用GCD说明后台任务?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。