iphone – 异步函数执行?

栏目: IOS · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/6225532/asynchronous-function-execution

在我的iOS应用程序中,我执行以下操作.

viewDidAppear(){

   // Load a spinner in a view on the top
   [DSBezelActivityView newActivityViewForView:self.view]; 
   // Execute code that require 3 seconds
   ...
   // Stop the spinner
   [DSBezelActivityView removeViewAnimated:YES];
}

问题是spinner没有出现,因为cpu正在努力工作(类似的东西).这就像开始和停止之间的代码优先于视图的渲染.

我很想找到一种方法来有效地显示微调器的开始,而不使用计时器来延迟代码执行.

谢谢

如果你有像这样的方法

-(void) showSpinner:(UIView*)view {
    dispatch_async(dispatch_get_main_queue(), ^{
        [DSBezelActivityView newActivityViewForView:view];
    });
}

有几种方法可以从不同的线程调用它.从以下选择一个:

[NSThread detachNewThreadSelector:@selector(showSpinner:) toTarget:self withObject:self.view];
// or 
[self performSelectorInBackground:@selector(showSpinner:) withObject:self.view];
// or 
NSInvocationOperation *invOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(showSpinner:) object:self.view];
NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
[opQueue addOperation:invOperation];
// or 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self showSpinner:self.view];
});

按住Alt键查看详细信息

翻译自:https://stackoverflow.com/questions/6225532/asynchronous-function-execution


以上所述就是小编给大家介绍的《iphone – 异步函数执行?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Developing Large Web Applications

Developing Large Web Applications

Kyle Loudon / Yahoo Press / 2010-3-15 / USD 34.99

As web applications grow, so do the challenges. These applications need to live up to demanding performance requirements, and be reliable around the clock every day of the year. And they need to withs......一起来看看 《Developing Large Web Applications》 这本书的介绍吧!

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具